【问题标题】:Why this jquery selector gives me error? uncaught exception: Syntax error, unrecognized expression: ''为什么这个 jquery 选择器给我错误?未捕获的异常:语法错误,无法识别的表达式:''
【发布时间】:2011-08-04 10:49:09
【问题描述】:

我正在使用此代码动态选择元素的属性,但它在 firebug 中给我一个错误

错误:

未捕获的异常:语法错误,无法识别的表达式:''

这是我的代码:

jQuery('.mydata').click(function(){

    var current_id=jQuery(this).attr('id');
    var current_datatype=jQuery(this).attr('datatype');

    var next_id=parseInt(current_id);

    next_id=next_id+1;

    next_id="'#"+next_id+"'";

    var next_datatype=jQuery(next_id).attr('datatype'); //this line gives error

});

【问题讨论】:

  • 每个答案都是正确的,我应该接受哪一个?

标签: javascript jquery


【解决方案1】:

如果你这样做了会怎样

next_id = "#" + next_id; 而不是next_id="'#"+next_id+"'";

【讨论】:

    【解决方案2】:

    选择ID时,如果将其分配给变量

    ,则不需要引号

    改变:

    next_id="'#"+next_id+"'";
    var next_datatype=jQuery(next_id).attr('datatype'); //this line gives error
    

    进入:

    next_id="#"+next_id;
    var next_datatype=jQuery(next_id).attr('datatype'); //this line gives error
    

    【讨论】:

      【解决方案3】:

      我认为您不需要这里的额外引号

      next_id="'#"+next_id+"'";
      

      应该阅读

      next_id="#"+next_id;
      

      【讨论】:

        【解决方案4】:

        改变这个...

        next_id="'#"+next_id+"'";
        

        到这个...

        next_id="#"+next_id;
        

        【讨论】:

          【解决方案5】:

          你的选择器变成'#2',而不是#2。您需要删除多余的 '.此外,如果不传入基数,则永远不要使用 parseInt,例如 parseInt(currentId, 10)。

          【讨论】:

            猜你喜欢
            • 2011-12-03
            • 1970-01-01
            • 2013-10-10
            • 2017-06-06
            • 2019-02-18
            • 2017-11-24
            • 2015-11-13
            • 2011-11-13
            • 1970-01-01
            相关资源
            最近更新 更多