jQuery 语法错误,无法识别 href 的表达式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31197452/
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 for href
提问by TDG
When I add below script and run. I am getting this:
当我添加以下脚本并运行时。我得到这个:
Uncaught Error: Syntax error, unrecognized expression: ul li a[href=#!id1]
未捕获的错误:语法错误,无法识别的表达式:ul li a[href=#!id1]
I am not sure which double quote causing the issue.
我不确定是哪个双引号导致了这个问题。
HTML
HTML
<ul>
<li class="slist selected" id="id1"><a href="#!id10">Test1/a></li>
<li class="slist" id="id2"><a href="#!id20">Test2</a></li>
<li class="slist" id="id3"><a href="#!id30">Test3/a></li>
</ul>
JS
JS
$(document).ready(function () {
var id = "#!" + window.location.href.split("!")[1];
if ($("ul li a[href=" + id + "]").length) {
console.log("present");
} else {
console.log("absent")
}
});
回答by Shaunak D
You need to enclose special characters in quotes when using a attribute based selector.
使用基于属性的选择器时,您需要将特殊字符括在引号中。
if ($('ul li a[href="' + id + '"]').length) {
Your version of selector would result
您的选择器版本将导致
if ($("ul li a[href=#!...]").length) {
The #!
will throw unrecognized expression.
该#!
会抛出无法识别的表达。
My version where the ""
escape the characters
我的版本在哪里""
转义字符
if ($('ul li a[href="#!..."]').length) {
回答by Bikram Shrestha
I tried the solution provided by
我尝试了提供的解决方案
https://github.com/jquery/jquery/issues/2885
which worked for me. I search for [href=#] in js and replace with [href*=\\#]
这对我有用。我搜索[href=#] in js and replace with [href*=\\#]
a[href*=\#]:not([href=\#])
回答by Sebastian Diaz
you may add the below code in functions.php
您可以在functions.php中添加以下代码
function modify_jquery() {
if (!is_admin()) {
wp_deregister_script('jquery');
wp_register_script('jquery', 'https://code.jquery.com/jquery-1.11.3.min.js');
wp_enqueue_script('jquery');
}
}
add_action('init', 'modify_jquery');