2011-03-27 50 views
0
function EditorDivOpen(a){ 
    $('#'+a).show(); 
} 

function EditorDivFill(a,b){ 
    var init; 
    if(a == 'smileys'){ 
     init = '<img src="data/img/smileys/sad.png" onclick="EditorTextFill(\':(\');" class="pointer" alt="smiley"> 
       <img src="data/img/smileys/happy.gif" onclick="EditorTextFill(\':)\');" class="pointer" alt="smiley"> 
       <img src="data/img/smileys/angelnot.gif" onclick="EditorTextFill(\'0:=)\');" class="pointer" alt="smiley"> 
       <img src="data/img/smileys/angry.gif" onclick="EditorTextFill(\'=(\');" class="pointer" alt="smiley"> 
       <img src="data/img/smileys/blush.gif" onclick="EditorTextFill(\':@\');" class="pointer" alt="smiley"> 
       <img src="data/img/smileys/cencored.png" onclick="EditorTextFill(\':*\');" class="pointer" alt="smiley"> 
       <img src="data/img/smileys/confused.gif" onclick="EditorTextFill(\':?\');" class="pointer" alt="smiley"> 
       <img src="data/img/smileys/cry.png" onclick="EditorTextFill(\';(\');" class="pointer" alt="smiley"> 
       <img src="data/img/smileys/grin.gif" onclick="EditorTextFill(\':D\');" class="pointer" alt="smiley"> 
       <img src="data/img/smileys/hmm.png" onclick="EditorTextFill(\':|\');" class="pointer" alt="smiley"> 
       <img src="data/img/smileys/hypocrite.gif" onclick="EditorTextFill(\'0=)\');" class="pointer" alt="smiley"> 
       <img src="data/img/smileys/lock.gif" onclick="EditorTextFill(\':x:\');" class="pointer" alt="smiley"> 
       <img src="data/img/smileys/love.gif" onclick="EditorTextFill(\':3\');" class="pointer" alt="smiley"> 
       <img src="data/img/smileys/sunglasses.gif" onclick="EditorTextFill(\'8)\');" class="pointer" alt="smiley"> 
       <img src="data/img/smileys/shifty.gif" onclick="EditorTextFill(\'|)\');" class="pointer" alt="smiley"> 
       <img src="data/img/smileys/shock.gif" onclick="EditorTextFill(\'O_o\');" class="pointer" alt="smiley"> 
       <img src="data/img/smileys/shock.gif" onclick="EditorTextFill(\':o\');" class="pointer" alt="smiley"> 
       <img src="data/img/smileys/sweatingbullets.gif" onclick="EditorTextFill(\'^_^\');" class="pointer" alt="smiley"> 
       <img src="data/img/smileys/tongue.gif" onclick="EditorTextFill(\':p\');" class="pointer" alt="smiley"> 
       <img src="data/img/smileys/tongue.gif" onclick="EditorTextFill(\':P\');" class="pointer" alt="smiley"> 
       <img src="data/img/smileys/wink.gif" onclick="EditorTextFill(\';)\');" class="pointer" alt="smiley"> 
       <img src="data/img/smileys/wry.gif" onclick="EditorTextFill(\'xo\');" class="pointer" alt="smiley"> 
       <img src="data/img/smileys/wry.gif" onclick="EditorTextFill(\'XD\');" class="pointer" alt="smiley"> 
       <img src="data/img/smileys/wry.gif" onclick="EditorTextFill(\'xD\');" class="pointer" alt="smiley">'; 
    } 
    else 
    { 
     init = '<b>[b][/b]</b>, <u>[u][/u]</u>, <i>[i][/i]</i>, <s>[s][/s]</s>, [img][/img], [url][/url].'; 
    } 

    $('#'+b).html(init); 
} 


      <img src="data/img/smileys/happy.gif" alt="smileys" onclick="EditorDivOpen('edit-content'); EditorDivFill('smileys', 'edit-content');" class="pointer"> 
      <div id="edit-content"> 
      </div> 

你好又一次stackoverflow!Javascript(jQ)問題:Div不會打開?

我仍在編輯我的論壇軟件。首先是自制的,然後是wysiwyg,現在我正在自己的工作。好吧,沒關係。

我在上面的代碼中得到了一些Javascript錯誤。在IE中的調試器說,「字符串常量未完成」的笑臉在該行的舊標籤的我: init = '<img src="data/img/smileys/sad.png" onclick="EditorTextFill(\':(\');" class="pointer" alt="smiley">

任何進一步的幫助將是不錯的。 Here's also a JsFiddle

回答

3

的JavaScript不允許在那裏,告訴它沒有一個指標多行字符串常量是,一個簡單的解決方案是增加\到每個行的,就像這樣:

init = '<img src="data/img/smileys/sad.png" onclick="EditorTextFill(\':(\');" class="pointer" alt="smiley"> \ 
     <img src="data/img/smileys/happy.gif" onclick="EditorTextFill(\':)\');" class="pointer" alt="smiley"> \ 
     <img src="data/img/smileys/angelnot.gif" onclick="EditorTextFill(\'0:=)\');" class="pointer" alt="smiley"> \ 
     <img src="data/img/smileys/angry.gif" onclick="EditorTextFill(\'=(\');" class="pointer" alt="smiley"> \ 
     <img src="data/img/smileys/blush.gif" onclick="EditorTextFill(\':@\');" class="pointer" alt="smiley"> \ 
     <img src="data/img/smileys/cencored.png" onclick="EditorTextFill(\':*\');" class="pointer" alt="smiley"> \ 
     <img src="data/img/smileys/confused.gif" onclick="EditorTextFill(\':?\');" class="pointer" alt="smiley"> \ 
     <img src="data/img/smileys/cry.png" onclick="EditorTextFill(\';(\');" class="pointer" alt="smiley"> \ 
     <img src="data/img/smileys/grin.gif" onclick="EditorTextFill(\':D\');" class="pointer" alt="smiley"> \ 
     <img src="data/img/smileys/hmm.png" onclick="EditorTextFill(\':|\');" class="pointer" alt="smiley"> \ 
     <img src="data/img/smileys/hypocrite.gif" onclick="EditorTextFill(\'0=)\');" class="pointer" alt="smiley"> \ 
     <img src="data/img/smileys/lock.gif" onclick="EditorTextFill(\':x:\');" class="pointer" alt="smiley"> \ 
     <img src="data/img/smileys/love.gif" onclick="EditorTextFill(\':3\');" class="pointer" alt="smiley"> \ 
     <img src="data/img/smileys/sunglasses.gif" onclick="EditorTextFill(\'8)\');" class="pointer" alt="smiley"> \ 
     <img src="data/img/smileys/shifty.gif" onclick="EditorTextFill(\'|)\');" class="pointer" alt="smiley"> \ 
     <img src="data/img/smileys/shock.gif" onclick="EditorTextFill(\'O_o\');" class="pointer" alt="smiley"> \ 
     <img src="data/img/smileys/shock.gif" onclick="EditorTextFill(\':o\');" class="pointer" alt="smiley"> \ 
     <img src="data/img/smileys/sweatingbullets.gif" onclick="EditorTextFill(\'^_^\');" class="pointer" alt="smiley"> \ 
     <img src="data/img/smileys/tongue.gif" onclick="EditorTextFill(\':p\');" class="pointer" alt="smiley"> \ 
     <img src="data/img/smileys/tongue.gif" onclick="EditorTextFill(\':P\');" class="pointer" alt="smiley"> \ 
     <img src="data/img/smileys/wink.gif" onclick="EditorTextFill(\';)\');" class="pointer" alt="smiley"> \ 
     <img src="data/img/smileys/wry.gif" onclick="EditorTextFill(\'xo\');" class="pointer" alt="smiley"> \ 
     <img src="data/img/smileys/wry.gif" onclick="EditorTextFill(\'XD\');" class="pointer" alt="smiley"> \ 
     <img src="data/img/smileys/wry.gif" onclick="EditorTextFill(\'xD\');" class="pointer" alt="smiley">'; 

You can try your fiddle here(通知還是有錯誤,但它不EditorDivOpen開始定義)。

+0

真棒,它的工作!謝謝:) – Thew 2011-03-27 12:38:54

+0

@Thew - 請檢查@ Pointy的建議,一組圖像名稱/編輯器文本數組在這裏非常出色,並且大大減少了您的標記,然後您可以遍歷該數組並創建''元素和分配處理程序。 – 2011-03-27 12:43:50

1

您不能跨線邊界繼續使用字符串。然而,你可以這樣做:

init = 'big long string that really should be done a totally different way' + 
    'more stuff that should be done differently' + 
    'still more ...'; 

把圖像名稱到一個數組,然後寫一小段代碼來創建的元素。停止使用DOM0事件處理程序設置(「onclick」)並使用jQuery「.delegate()」來處理圖像上的點擊。

(OK所以@Nick指出,你可以在「引用」行分隔符用反斜槓作爲替代。)

+0

當然,你可以看到我的答案;) – 2011-03-27 12:37:11

+0

@Nick做舊的IE瀏覽器的允許?我腦海中有一個古老的事實,這會導致問題,但當然是在這種印象之下,我還沒有嘗試在十年或更長時間內做這件事:-) – Pointy 2011-03-27 12:38:07

+0

只要我記得,它一直存在......但我沒有IE5測試... :) – 2011-03-27 12:38:36

0

對於JavaScript的上帝的份上,至少有這樣的事情,而不是:

var smileyData = [ 
    {image: "sad.png", shortcuts: [":("]}, 
    {image: "happy.gif", shortcuts: [":)"]}, 
    {image: "wry.gif", shortcuts: ["xo", "xd", "xD"]} 
]; 
if(a == 'smileys'){ 
    var arrImages = []; 
    $.each(smileyData, function(index, data) { 
     var sImage = data["image"]; 
     $.each(data["shortcuts"], function (i2, shortcut) { 
      arrImages.push('<img src="data/img/smileys/' + sImage + '" onclick="EditorTextFill(\'' + shortcut + '\');" class="pointer" alt="smiley" />'); 
     }); 
    }); 
    init = arrImages.join(""); 
} 

這不正是尖尖說這是理想的方式,但它仍然會爲您節省大量的頭痛 - 將項目添加到smileyData陣列將比重複相同的HTML一遍又一遍簡單得多。

這與現在一樣,只是以不同的方式書寫,在我看來更好更易於管理。 :)

編輯的jsfiddle玩:http://jsfiddle.net/VTFvB/1/