2015-09-03 20 views
1

我得到一個可愛的語法錯誤。這是當我嘗試選擇所有沒有包含佔位符URL的href的錨標記時,即href="#"jQuery:不選擇符和哈希符號語法錯誤

我已經試過如下:

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

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

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

// wildcard selector 
$("a:not(href*=#)"); 

,他們都導致以下錯誤:

Uncaught Error: Syntax error, unrecognized expression: href=#(…)

任何想法?


摘錄如下:

var $item = $("a:not(href='#')"); 
 
console.log($item);
<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>

+0

@Vohuman我做了。我只是忽略了它。 – evolutionxbox

回答

5

href需要在方括號中的測試:

$("a:not([href='#'])") 

括號是有,因爲這是你如何在中做一個屬性值測試CSS選擇器。例如,如果您只是在尋找具有href的元素,那麼它看起來像

$("[href='#']") 
+0

謝謝!我想我錯過了這麼簡單的事情。 – evolutionxbox