admin管理员组文章数量:1310513
I create WordPress ShortCode tab and write this code to collect the 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);
});
and i get this error
Uncaught SyntaxError: Unexpected token if
and i try the code in / and i got this error in the line which have this code
shortcode += '[tap ';
Expected an assignment or function call and instead saw an expression.
how to fix it ?
I create WordPress ShortCode tab and write this code to collect the 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);
});
and i get this error
Uncaught SyntaxError: Unexpected token if
and i try the code in http://jsfiddle/ and i got this error in the line which have this code
shortcode += '[tap ';
Expected an assignment or function call and instead saw an expression.
how to fix it ?
Share Improve this question asked Aug 26, 2013 at 20:11 Adam Mo.Adam Mo. 7722 gold badges12 silver badges26 bronze badges 1-
1
Remove this
shortcode += '[tap ';
from the var define chain. Otherwise you define it again and there is no value to add to. – Sergio Commented Aug 26, 2013 at 20:12
2 Answers
Reset to default 4When you have
var title = jQuery('.Title').val(),
content = jQuery('.Content').val(),
shortcode += '[tap ';
you are defining new variables in that chain but shortcode
is already defined, so you are creating a new variable in this scope. Being a new variable you cannot use +=
. Anyway I think you just want to use this:
var title = jQuery('.Title').val(),
content = jQuery('.Content').val(); // changed the last ma with semicolon
shortcode += '[tap ';
To read:
About scope
About var
Problem is ing in here
var title = jQuery('.Title').val(),
content = jQuery('.Content').val(),
shortcode += '[tap ';
shortcode
is already a var defined above. You can't use +=
in a var
expression
Just change this to
var title = jQuery('.Title').val(),
content = jQuery('.Content').val(); // note the semicolon here
shortcode += '[tap ';
I think you're also going to run into some nesting issue. Instead of calling jQuery('.Content').val()
for each iteration of the loop, I think you're looking for something more like $(this).find('.Content').val()
or $('.Content', this)
. This will find the relevant .Content
input in the scope of a given .tapForm
.
I'm thinking something like this, but it's just an idea
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]');
});
本文标签: javascriptUncaught SyntaxError Unexpected token ifStack Overflow
版权声明:本文标题:javascript - Uncaught SyntaxError: Unexpected token if - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741818055a2399194.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论