【问题标题】:jQuery :not selector and hash symbol syntax errorjQuery:不是选择器和哈希符号语法错误
【发布时间】:2015-11-29 09:40:40
【问题描述】:

我遇到了一个可爱的语法错误。当我尝试选择所有没有包含占位符 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*=#)");

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

未捕获的错误:语法错误,无法识别的表达式: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>

【问题讨论】:

  • @Vohuman 我做到了。我只是忽略了它。

标签: javascript jquery jquery-selectors


【解决方案1】:

href 的测试需要放在方括号中:

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

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

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

【讨论】:

  • 谢谢!太认为我错过了这么简单的事情。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-27
  • 2015-05-27
相关资源
最近更新 更多