<body>
<div>
<p>This is <strong>first</strong> paragraph</p>
<p>And this one is second</p>
<span><h2>I am p inside span</h2></span>
<h1>I am h1</h1>
</div>
<div name="divName">
<p><a href="www.google.pl">This is a paragraph in second div</a></p>
</div>
</body>
下面的代码给我:
Error: Syntax error, unrecognized expression: a[@href]
//Another file
$(document).ready(function() {
$("div[p]").css("background-color", "green");
$("a[@href]").css("background-color", "yellow");
});
还有 $("div[p]").css("background-color", "green");
似乎什么也没做。
这里发生了什么?
根据http://www.tutorialspoint.com/jquery/jquery-selectors.htm这些应该是有效的。
最佳答案
不清楚“tutorialspoint”使用的是哪个版本的 jquery,但是,假设您指的是数字 17:
$("div[p]")
Selects all elements matched by that contain an element matched by
<p>
这要么是完全错误的,要么是非常过时的(编辑:看起来它已经过时了大约 10 年......请参阅下面的编辑)。
SO 上也有很多问题询问如何实现这一点,但没有一个给出这个答案。
您应该引用的页面是:
https://api.jquery.com/category/selectors/
这表明 []
用于匹配属性,eg:
<p data-id='123'>
$("p[data-id]")
将匹配所有 p
具有属性 data-id
(无论在这种情况下的值(value))。
编辑:解决标题“unrecognised expresion a[@href]”中的特定问题 - 请参阅此问题:What does the "@" sign in jQuery selector means?答案表明这是 2010 中已过时的“2 个版本之前”
所以我猜你的教程页面已经过时了 10 年
关于javascript - Jquery 选择器不工作 : Error: Syntax error, 无法识别的表达式:a[@href],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32385685/