jQuery:对所有元素起作用,但<a>

时间:2015-12-17 09:28:49

标签: jquery jquery-selectors

I would like to run a function on the whole website but not on "a"s. Problem is, that the "a"s are inside "div"s, so selection like

$(':not(a)').myFunction();

does not work (since it is called on the parent-div). Sounds like a simple problem, I just don't seem to find a solution. Thanks!

3 个答案:

答案 0 :(得分:3)

$('body > *').not(':has("a")').myFunction();

您可以将not()部分替换为您想要保留的内容。以上内容适用于您的示例。

答案 1 :(得分:0)

您应该使用.not()函数执行此操作:

$('*').not('a').myFunction();

http://api.jquery.com/not/

答案 2 :(得分:0)

我现在通过解除'a'上的功能来实现它。

$('a').hover(
        function () {
            $('#tooltip').hide().addClass("hover");
...

$("body").on( "click", function() {
    ....
});

$("a").click(function() {
    $("body").off("click");
});