这就是我如何选择所有 href 不以 #
开头的链接:
$('a:not([href^="#"])')
但是如何确定链接是否具有以井号标签开头的 href AFTER 它被单击?这失败了:
$('a').on('click', function() {
if($(this).is('[href^="#"])')) {
alert('No hash');
}
});
最佳答案
您从选择器中删除了 :not()
。放回去,它应该可以工作。
if($(this).is('a:not([href^="#"])')) {
演示: http://jsfiddle.net/8An48/
或者更好...
if (this.getAttribute("href").charAt(0) !== "#") {
演示: http://jsfiddle.net/8An48/1/
或者使用来自最近的祖先的事件委托(delegate),其中包含所有被定位的 a
元素。
$('body').on('click', 'a:not([href^="#"])', function() {
alert('No hash');
});
关于javascript - 检查点击的链接 href 是否不以哈希标签开头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20251493/