简体   繁体   English

未捕获的SyntaxError:意外的令牌if

[英]Uncaught SyntaxError: Unexpected token if

I create WordPress ShortCode tab and write this code to collect the shortcode 我创建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);
});

and i get this error 我得到这个错误

Uncaught SyntaxError: Unexpected token if 

and i try the code in http://jsfiddle.net/ and i got this error in the line which have this code 我尝试了http://jsfiddle.net/中的代码,我在这行代码中得到了这个错误

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

how to fix it ? 怎么解决?

When 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. 您正在该链中定义新变量,但已定义了shortcode ,因此您要在此范围内创建新变量。 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 comma with semicolon
shortcode += '[tap ';

To read: 阅读:
About scope 关于范围
About var 关于var

Problem is coming in here 问题就在这里

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

shortcode is already a var defined above. shortcode已经是上面定义的var。 You can't use += in a var expression 你不能在var表达式中使用+=

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) . 而不是为循环的每次迭代调用jQuery('.Content').val() ,我认为你正在寻找更像$(this).find('.Content').val()$('.Content', this) This will find the relevant .Content input in the scope of a given .tapForm . 这将在给定的.tapForm范围内找到相关的.Content输入。

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]');  
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM