[英]Uncaught SyntaxError: Unexpected token if
我創建WordPress ShortCode選項卡並編寫此代碼以收集短代碼
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);
});
我得到這個錯誤
Uncaught SyntaxError: Unexpected token if
我嘗試了http://jsfiddle.net/中的代碼,我在這行代碼中得到了這個錯誤
shortcode += '[tap ';
Expected an assignment or function call and instead saw an expression.
怎么解決?
當你有
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 ';
問題就在這里
var title = jQuery('.Title').val(),
content = jQuery('.Content').val(),
shortcode += '[tap ';
shortcode
已經是上面定義的var。 你不能在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]');
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.