Jquery语法错误,无法识别的表达式: [英] Jquery Syntax error, unrecognized expression:

查看:393
本文介绍了Jquery语法错误,无法识别的表达式:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我会收到此错误,如何测试它以便它不会破坏,我尝试检查null但显然不会工作,谢谢。

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.

请不建议不写这样的ID因为我知道错了但是有可能。

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

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

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

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


推荐答案

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

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

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

此正则表达式失败 '' '' '' \ \ \ \ '' '' '' '' '' '''\ ##### $ '' '' '或更简单地用于'

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

查询引擎有限对于如此简洁的语言和id有效性规则来说,这并不是很令人惊讶。它无法处理任何有效的身份证。

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.

如果你真的需要能够处理任何类型的有效身份证,请使用

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

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

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

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

这篇关于Jquery语法错误,无法识别的表达式:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆