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

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

JQUERY: Uncaught Error: Syntax error, unrecognized expression

jquery

提问by Priya

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

in html i have

在 html 我有

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

It throws error:

它抛出错误:

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

未捕获的错误:语法错误,无法识别的表达式:“#2013-10-23”

In above code i have one div with id="2013-10-23"and when getting that id in response it is throwing syntax error

在上面的代码中,我有一个 div,id="2013-10-23"并且在获取该 id 作为响应时,它会抛出语法错误

回答by webduvet

try

尝试

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

your solution is passing the double quotes as part of the string.

您的解决方案是将双引号作为字符串的一部分传递。

回答by Martijn

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 */ }

回答by Sunil Verma

Try using:

尝试使用:

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

This will remove the extra quotes you were using.

这将删除您使用的额外引号。

回答by OneThreeSeven

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 中工作。

回答by RDK

Try this (ES5)

试试这个(ES5)

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

ES6

ES6

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

回答by Fendi Septiawan

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. It makes your code simpler.

或者,您可以在使用前定义 var。它使您的代码更简单。

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