a:不是(a:不是([href]))选择器 [英] a:not(a:not([href])) selector
问题描述
我希望每当 href
属性时,某个动作与锚标记的click事件相关联:
I want that a certain action to be associated to the click event of the anchor tag whenever its href
attribute:
- 不以
mailto开头:
和 - 不以
#
和 - 包含任何值,包括空
所以我正在尝试这段代码:
So I was trying this code:
<a href="example.com">example.com</a>
<a href="mailto:someone@www.example.com">Someone</a>
<a href="thepage#">The Page</a>
<a>This does nothing</a>
<script type="text/javascript">
$(document).on('click', 'a:not([href^="mailto\\:"], [href$="\\#"], [href])', myFunction);
</script>
它不起作用。但让我感到沮丧的是:
And it didn't work. But to my dismay this works:
$(document).on('click', 'a:not([href^="mailto\\:"], [href$="\\#"], a:not([href]))', myFunction);
但我不明白怎么做。注意内部:not()
。据我所知, [href]
表示没有 href
属性。或者它是相反的?
But I don't understand how. Notice the inner :not()
. As I understand the [href]
means there is no href
attribute. Or is it the opposite?
有人能引导我走向光明吗?
Could someone lead me to the light?
推荐答案
[href]
表示有一个href属性,无论它具有哪个值(如果有)。
[href]
means there is an href attribute no matter which, if any, value it has.
正如pimvdb正确指出的那样,你可以使用
As pimvdb correctly pointed out, you could use
a [href]:not([href ^ =mailto \\: ],[href $ =\\#])
这意味着所有具有任何href属性的元素,除了那些href属性以mailto:开头或以#
Which means "all a elements with any href attribute, except those whose href attribute starts with mailto: or ends with #
这篇关于a:不是(a:不是([href]))选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!