我尝试使用这个jQuery选择器:
$("a:has(href*=#)").click(function() {
alert('works');
});
但似乎它不起作用。我想选择所有href属性中带有锚点(即#符号)的<a>标签。
我尝试使用这个jQuery选择器:
$("a:has(href*=#)").click(function() {
alert('works');
});
但似乎它不起作用。我想选择所有href属性中带有锚点(即#符号)的<a>标签。
$("a[href*=#]").click(function(e) {
e.preventDefault();
alert('works');
});
*=
将筛选包含给定字符串的属性,不管其位置在哪里
$("a[href*='#']").click(function() {
alert('works');
});
同时请注意
$("a[href^='#']").click(function() {
alert('works');
});
将选择任何 href 属性以 #
开头的锚点。
^=
选择器,这就是我一直在寻找的! - calvinf<a href='#'>
的情况,而不是像<a href='index.php#header'>
这样的情况。另外,感谢您指出引用=D的问题。 - Korvin Szanto#header
,而我想到的方法是选择所有在href
中带有#
的<a>
。 - simPod$('a[href=^"#"]')
来防止选择像http://en.wikipedia.org/wiki/Random#A_number_is_.22due.22
这样的href。我已更新我的答案! - Korvin Szanto^
符号应该在等号之前。 - chodorowicz
<a href='#'>
的情况,而不是<a href='link.com/page/#hash'>
的情况。 - Korvin Szantoa[href*=#]
不起作用。 - maheshwaghmare#
:$('a[href*=\\#]')
。 - lurkit