javascript 语法错误,无法识别的表达式:, $(selector).before(",");
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6295576/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Syntax error, unrecognized expression: , $(selector).before(",");
提问by Dan
I've been using this one line of code for a couple of years now with jQuery 1.2.6.
我已经在 jQuery 1.2.6 中使用这一行代码几年了。
$("#acListTemp div.amenitiesDiv label").before(",");
I just upgraded to jQuery 1.6.1 and now it is giving me this error:
我刚刚升级到 jQuery 1.6.1,现在它给了我这个错误:
Syntax error, unrecognized expression: ,
语法错误,无法识别的表达式:,
I also tried this, but it yielded the same error:
我也试过这个,但它产生了同样的错误:
theChar = ",";
$("#acListTemp div.amenitiesDiv label").before(theChar);
I checked the jQuery API page for the before command, but I'm still stumped. Any help is greatly appreciated!
我检查了 jQuery API 页面的 before 命令,但我仍然很难过。任何帮助是极大的赞赏!
回答by user113716
If you're using .insertBefore()
instead of .before()
, you'll get that error.
如果您使用的是.insertBefore()
而不是.before()
,则会出现该错误。
The error doesn't appear using insertBefore()
in jQuery 1.2.6
insertBefore()
在 jQuery 1.2.6 中使用时不会出现该错误
Looks like the error comes specifically from Sizzle:
看起来错误来自 Sizzle:
https://github.com/jquery/sizzle/blob/master/sizzle.js#L325-327
https://github.com/jquery/sizzle/blob/master/sizzle.js#L325-327
Sizzle.error = function( msg ) {
throw "Syntax error, unrecognized expression: " + msg;
};
...so I'd guess that somewhere in your code you're using ","
as a selector, perhaps in insertBefore()
or some other method that expects a selector.
...所以我猜你在代码中的某个地方","
用作选择器,可能是在insertBefore()
或其他一些需要选择器的方法中。
EDIT:
编辑:
From the comments below, it has been determined that jQuery does notfail silently as expected when calling .before()
against a jQuery object for which no matches are found for its selector.
从下面的评论中可以确定,当调用一个 jQuery 对象时,jQuery 并没有像预期的那样静默失败,.before()
而该对象的选择器没有找到匹配项。
Seems like a bug to me, but I'm sure the jQuery guys will come up with some reason why this is the desired behavior.
对我来说似乎是一个错误,但我相信 jQuery 人会想出一些原因,为什么这是所需的行为。
You can see the issue here: https://github.com/jquery/jquery/blob/1.6.1/src/manipulation.js#L130-140
你可以在这里看到这个问题:https: //github.com/jquery/jquery/blob/1.6.1/src/manipulation.js#L130-140
before: function() {
if ( this[0] && this[0].parentNode ) {
return this.domManip(arguments, false, function( elem ) {
this.parentNode.insertBefore( elem, this );
});
} else if ( arguments.length ) {
var set = jQuery(arguments[0]);
set.push.apply( set, this.toArray() );
return this.pushStack( set, "before", arguments );
}
},
As you can see, there are only 2 tests.
如您所见,只有 2 个测试。
The first checks to see if there is at least one element in the jQuery object (and if it has a parentNode). If so, it inserts the content before the matched elements.
If there were no elements, it just checks to see if any arguments were passed, and if so, it seems to assume that the first one is a selector (or perhaps a tag), and it goes ahead and sends that to the jQuery function.
第一个检查 jQuery 对象中是否至少有一个元素(以及它是否有 parentNode)。如果是,它会在匹配的元素之前插入内容。
如果没有元素,它只是检查是否有任何参数被传递,如果是,它似乎假设第一个是选择器(或者可能是一个标签),然后它继续将它发送到 jQuery 函数.
var set = jQuery(arguments[0]);
So if you're trying to insert a ","
, but your selector didn't find any matches, jQuery will end up doing:
因此,如果您尝试插入","
,但您的选择器没有找到任何匹配项,jQuery 将最终执行以下操作:
var set = jQuery(",");
...which is an invalid expression for Sizzle to process.
...这是 Sizzle 处理的无效表达式。