2013-08-26 33 views
4

我創建WordPress的簡碼標籤,寫這樣的代碼來收集簡碼未捕獲的SyntaxError:意外的標記,如果

jQuery('body').on('click', '#tapSubmit',function(){ 
    var shortcode = '[tapWrap]'; 
    jQuery('.tapForm').each(function(){ 
     var title = jQuery('.Title').val(), 
      content = jQuery('.Content').val(), 
      shortcode += '[tap '; 
     if(title){shortcode += 'title="'+title+'"';} 
     shortcode += ']'; 
     if(content){shortcode += ''+content+'';} 
     shortcode += '[/tap]'; 
    }); 
    shortcode += '[/tapWrap]'; 

    tinyMCE.activeEditor.execCommand('mceInsertContent', false, shortcode); 
}); 

和我得到這個錯誤

​​

,我嘗試在http://jsfiddle.net/和我的代碼得到這個錯誤的代碼行

shortcode += '[tap '; 
Expected an assignment or function call and instead saw an expression. 

如何解決它?

+1

刪除此'簡碼+ = '[點擊';'從VAR定義鏈。否則,你再次定義它,沒有值添加到。 – Sergio

+0

感謝它的工作:) –

回答

4

當你有

var title = jQuery('.Title').val(), 
     content = jQuery('.Content').val(), 
     shortcode += '[tap '; 

你在一個鏈定義新的變量,但shortcode已經定義,所以你正在創建一個新的變量在這個範圍內。作爲一個新的變量,你不能使用+=。無論如何,我認爲你只是想用這樣的:

var title = jQuery('.Title').val(), 
    content = jQuery('.Content').val(); // changed the last comma with semicolon 
shortcode += '[tap '; 

閱讀:
關於scope
關於var

3

問題是在來這裏

var title  = jQuery('.Title').val(), 
    content = jQuery('.Content').val(), 
    shortcode += '[tap '; 

shortcode已經是上述定義的變種。不能使用+=var表達

只是將其更改爲

var title  = jQuery('.Title').val(), 
    content = jQuery('.Content').val(); // note the semicolon here 

shortcode += '[tap '; 

我想你也將遇到一些嵌套問題。對於循環的每次迭代,我都不會調用jQuery('.Content').val(),我認爲您正在尋找更像$(this).find('.Content').val()$('.Content', this)的東西。這將在給定.tapForm的範圍內找到相關的.Content輸入。

我想這樣的事情,但它只是一個想法

jQuery('body').on('click', '#tapSubmit', function(){ 

    function title(context) { 
    var value = jQuery(".Title", context).val(); 
    return value ? 'title="' + value + '"' : ''; 
    } 

    function content(context) { 
    var value = jQuery(".Content", context).val(); 
    return value || ''; 
    } 

    var taps = jQuery('.tapForm').map(function(){ 
    return '[tap ' + title(this) + ']' + content(this) + '[/tap]'; 
    }).join(); 

    tinyMCE.activeEditor.execCommand('mceInsertContent', false, '[tapWrap]' + taps + '[/tapWrap]'); 
}); 
+1

我認爲你應該強調第二個'''必須用';'替換。通過查看代碼可能並不明顯。 –

+0

已註明並更新;謝謝@FelixKling。 – naomik

相關問題