jQuery:不是选择器和哈希符号语法错误

evo*_*box 1 javascript jquery jquery-selectors

我收到一个可爱的语法错误.当我尝试选择所有锚标签而没有包含占位符URL的href时,即href="#".

我尝试过以下方法:

$("a:not(href='#')");

//swapped single with double quotes
$('a:not(href="#")');

// no quotes at all
$("a:not(href=#)");

// wildcard selector
$("a:not(href*=#)");
Run Code Online (Sandbox Code Playgroud)

并且所有这些都会导致以下错误:

未捕获错误:语法错误,无法识别的表达式:href =#(...)

有任何想法吗?


下面的代码段:

var $item = $("a:not(href='#')");
console.log($item);
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<a href="http://google.com">Selected</a>
<a href="#">Not selected</a>
Run Code Online (Sandbox Code Playgroud)

Poi*_*nty 5

为测试href需要在方括号:

$("a:not([href='#'])")
Run Code Online (Sandbox Code Playgroud)

括号是因为这是你在CSS选择器中进行属性值测试的方式.href例如,如果您只是在寻找具有该元素的元素,那么它看起来就像

$("[href='#']")
Run Code Online (Sandbox Code Playgroud)