最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Uncaught SyntaxError: Unexpected token if - Stack Overflow

programmeradmin1浏览0评论

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
Add a ment  | 

2 Answers 2

Reset to default 4

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. 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]');  
});
发布评论

评论列表(0)

  1. 暂无评论