简体   繁体   English

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

[英]jQuery: Uncaught Error: Syntax error, unrecognized expression

console.log($('"#'+d+'"'));

In my HTML, I have:在我的 HTML 中,我有:

<div id="2013-10-23">
    <h1>5</h1>
    <p>eeeeeeeeeeee</p>
</div>

In the above code, I have one <div> with an id of 2013-10-23 , and when getting that id it is throwing this syntax error:在上面的代码中,我有一个id2013-10-23<div> ,当获取该id时,它会抛出以下语法错误:

 Uncaught Error: Syntax error, unrecognized expression: "#2013-10-23"

try尝试

console.log($("#"+d));

your solution is passing the double quotes as part of the string.您的解决方案是将双引号作为字符串的一部分传递。

The "double quote" + 'single quote' combo is not needed不需要“双引号”+“单引号”组合

console.log( $('#'+d) ); // single quotes only
console.log( $("#"+d) ); // double quotes only

Your selector results like this, which is overkill with the quotes:你的选择器结果是这样的,这对引号来说有点过分了:

$('"#abc"') // -> it'll try to find  <div id='"#abc"'>

// In css, this would be the equivalent:
"#abc"{ /* Wrong */ } // instead of:
#abc{ /* Right */ }

This can also happen in safari if you try a selector with a missing ], for example例如,如果您尝试使用缺少 ] 的选择器,这也可能在 safari 中发生

$('select[name="something"')

but interestingly, this same jquery selector with a missing bracket will work in chrome.但有趣的是,这个缺少括号的相同 jquery 选择器将在 chrome 中工作。

Try using:尝试使用:

console.log($("#"+d));

This will remove the extra quotes you were using.这将删除您使用的额外引号。

Try this (ES5)试试这个(ES5)

console.log($("#" +  d));

ES6 ES6

console.log($(`#${d}`));

I had to look a little more to solve my problem but what solved it was finding where the error was.我不得不多看一点来解决我的问题,但解决问题的是找到错误所在。 Here It shows how to do that in Jquery's error dump. 这里它展示了如何在 Jquery 的错误转储中做到这一点。

In my case id was empty and $("#" + id);在我的情况下id是空的并且$("#" + id); ; ; produces the error.产生错误。

It was where I wasn't looking so that helped pinpoint where it was so I could troubleshoot and fix it.这是我没有寻找的地方,因此有助于查明它的位置,以便我可以进行故障排除和修复。

If you're using jQuery 2.1.4 or above, try this:如果您使用的是 jQuery 2.1.4 或更高版本,请尝试以下操作:

$("#" + this.d);

Or, you can define var before using it.或者,您可以在使用之前定义 var。 It makes your code simpler.它使您的代码更简单。

var d = this.d
$("#" + d);

For some people coming here, you might have a special character in your id attribute, so jQuery can't read it correctly.对于一些来到这里的人,您的id属性中可能有一个特殊字符,因此 jQuery 无法正确读取它。

ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods ("."). ID 和 NAME 标记必须以字母 ([A-Za-z]) 开头,后跟任意数量的字母、数字 ([0-9])、连字符 ("-")、下划线 ("_") , 冒号 (":") 和句点 (".")。

Check this answer for more details: What are valid values for the id attribute in HTML?查看此答案以获取更多详细信息: HTML 中 id 属性的有效值是什么?

I had a selector with space ( '#test .test' ).我有一个带空格的选择器( '#test .test' )。

Example of my error:我的错误示​​例:

var href = "javascript:scrollto('"+key+"')";

For me this helped: encodeURIComponent (value)对我来说这有帮助: encodeURIComponent (value)

var href = "javascript:scrollto('"+encodeURIComponent(key)+"')";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未捕获的错误:语法错误,jQuery中无法识别的表达式 - Uncaught Error: Syntax error, unrecognized expression in jQuery 未捕获的错误、语法错误、无法识别的表达式 [JQUERY] - Uncaught Error, Syntax Error, unrecognized expression [JQUERY] jQuery Uncaught语法错误,无法识别的表达式:+ - jQuery Uncaught Syntax error, unrecognized expression: + Jquery 错误 - 未捕获的错误:语法错误,无法识别的表达式 - Jquery error - Uncaught Error: Syntax error, unrecognized expression jQuery Uncaught Error语法错误,无法识别的表达式:li / label - Jquery Uncaught Error Syntax error, unrecognized expression: li/label 未捕获的错误:语法错误,jQuery升级后无法识别表达式 - Uncaught Error: Syntax error, unrecognized expression after jQuery upgrade JQUERY 未捕获的错误:语法错误,无法识别的表达式:# on javascript: void(0) - JQUERY Uncaught Error: Syntax error, unrecognized expression: # on javascript: void(0) Phonegap / Cordova jQuery未捕获错误:语法错误,无法识别的表达式 - Phonegap/Cordova jQuery Uncaught Error: Syntax error, unrecognized expression JQuery JS未捕获错误:语法错误,无法识别的表达式: - JQuery JS Uncaught Error: Syntax error, unrecognized expression: Bootstrap 下拉菜单 Jquery Uncaught Error: Syntax error, unrecognized expression - Bootstrap dropdown Jquery Uncaught Error: Syntax error, unrecognized expression
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM