Jquery 语法错误,无法识别的表达式:

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15338727/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 14:50:50  来源:igfitidea点击:

Jquery Syntax error, unrecognized expression:

javascriptjqueryjquery-selectors

提问by Hello-World

Why do I get this error and how can I test for it so it wont break, I tried checking for null but obviously that wont work, thanks.

为什么我会收到此错误,以及如何测试它以使其不会中断,我尝试检查 null 但显然这不起作用,谢谢。

Please don't advice to not write the ID like this as I know its wrong but it is a possibility.

请不要建议不要写这样的 ID,因为我知道它是错误的,但这是可能的。

var jsonTest = [
  {
    "myId": "''''''\"\"\"\"'''''''''''''\"#####$'''''",
  }
];

alert(jsonTest[0].myId); 
// Works - alerts the myId

$('#' + jsonTest[0].myId ).length; 
// Error: Syntax error, unrecognized expression:
// #''''''""""'''''''''''''"#####$'''''

回答by Denys Séguret

jQuery's uses this code to detect an id based selector :

jQuery 使用此代码来检测基于 id 的选择器:

characterEncoding = "(?:\\.|[\w-]|[^\x00-\xa0])+"
...
"ID": new RegExp( "^#(" + characterEncoding + ")" ),

This regex fails for "''''''\"\"\"\"'''''''''''''\"#####$'''''"or more simply for "'".

此正则表达式失败"''''''\"\"\"\"'''''''''''''\"#####$'''''"或更简单地为"'".

The query engine is limited, which isn't very surprising for a so concise language and id validity rules so lax. it can't handle any valid id.

查询引擎是有限的,对于如此简洁的语言和如此松散的id有效性规则来说,这并不奇怪。它无法处理任何有效的 ID。

If you really need to be able to handle any kind of valid id, use

如果您确实需要能够处理任何类型的有效 ID,请使用

$(document.getElementById(jsonTest[0].myId))

In fact, you should never use $('#'+id)as it simply adds a useless (and a little dangerous) layer of parsing for the same operation.

事实上,你永远不应该使用$('#'+id)它,因为它只是为相同的操作添加了一个无用(而且有点危险)的解析层。

回答by álvaro González

If I understood your question, you have an ID that contains special characters. You basically need to escape themso they're treated as literal characters rather than query selectors:

如果我理解你的问题,你有一个包含特殊字符的 ID。您基本上需要对它们进行转义,以便将它们视为文字字符而不是查询选择器:

To use any of the meta-characters ( such as !"#$%&'()*+,./:;<=>?@[]^`{|}~ ) as a literal part of a name, it must be escaped with with two backslashes: \. For example, an element with id="foo.bar", can use the selector $("#foo\.bar").

要使用任何元字符(例如 !"#$%&'()*+,./:;<=>?@[]^`{|}~ )作为名称的文字部分,它必须用两个反斜杠转义:\。例​​如,id="foo.bar" 的元素可以使用选择器 $("#foo\.bar")。

... and in this case you also need an additional escaping level for the string delimiter, ". It's crazy but this works:

...在这种情况下,您还需要为字符串定界符". 这很疯狂,但这有效:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head><title></title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript"><!--
jQuery(function($){
    console.log( $("#" + "\'\'\'\'\'\'\\"\\"\\"\\"\'\'\'\'\'\'\'\'\'\'\'\'\'\\"\#\#\#\#\#\$\'\'\'\'\'").length );
});
//--></script>
</head>
<body>

<div id="''''''&quot;&quot;&quot;&quot;'''''''''''''&quot;#####$'''''">Foo</div>

</body>
</html>

Edit:Of course, dystroy's answer —omit jQuery selection engine and use getElementByID()— is way more practical. jQuery manual links an interesting blog entrythat covers this and other related techniques:

编辑:当然,dystroy 的答案——省略 jQuery 选择引擎和使用getElementByID()——更实用。jQuery 手册链接了一个有趣的博客条目,其中涵盖了此技术和其他相关技术:

document.getElementById() and similar functions like document.getElementsByClassName() can just use the unescaped attribute value, the way it's used in the HTML. Of course, you would have to escape any quotes so that you still end up with a valid JavaScript string.

document.getElementById() 和类似的函数,如 document.getElementsByClassName() 可以只使用未转义的属性值,它在 HTML 中的使用方式。当然,您必须对任何引号进行转义,以便最终得到一个有效的 JavaScript 字符串。

回答by Stan James

W3C has defined a new function for this this: CSS.escape(). In your example code, it would look like this:

W3C 为此定义了一个新函数:CSS.escape(). 在您的示例代码中,它看起来像这样:

var jsonTest = [
  {
    "myId": "''''''\"\"\"\"'''''''''''''\"#####$'''''",
  }
];

$('#' + CSS.escape(jsonTest[0].myId)).length; // Works 

Browsers don't yet support it, but there is a polyfill library that you can use in the mean time: https://github.com/mathiasbynens/CSS.escape

浏览器尚不支持它,但您可以同时使用一个 polyfill 库:https: //github.com/mathiasbynens/CSS.escape

I've used it with success in my project.

我在我的项目中成功地使用了它。