0

I am attempting to change the css of menu items by clicking anywhere else on the screen. This is to reset they're css properties after already clicking on them and changing the css from they're defaults.

If I have a list item with id="linkitem" how to I click on anything but #linkitem to change the css of it.

Thanks

4

5 回答 5

1
$('li').not('#linkitem').click(function(){
    //change css here
});

和说明这种行为的小提琴

以及 jQuery.not() 的文档

于 2013-05-13T17:32:29.840 回答
1

你需要一个否定选择器

$(':not(#linkitem)').click(function(){
    //insert code here
});
于 2013-05-13T17:32:38.913 回答
1
$('body').on('click', function(e) {
    if(e.target.id === 'linkitem'){
       // Do something
    }
    else {
       // something else
    }
});
于 2013-05-13T17:32:47.290 回答
1
$(document).on('click', function(e) {
    if ( ! $(e.target).closest('#linkitem').length ) {
        // you clicked anywhere but on an element inside #linkitem (or itself)
        $('#linkitem').css('color', 'red');
    }
});
于 2013-05-13T17:32:54.307 回答
1

我不确定你的意思,但你可以简单地做:

$(":not(#linkitem)").on('click',function(){
    alert("I was callled");
}
于 2013-05-13T17:33:30.407 回答