$('html').click()… anywhere except one element

前端 未结 4 1806
孤城傲影
孤城傲影 2021-02-05 05:52

I have a dynamically appended menu which I am removing if you click anywhere on page including the menu links itself. What I am trying to achieve is to prevent the remove if yo

相关标签:
4条回答
  • 2021-02-05 06:05
    $('html').click(function(e) {
    
            /* exept elements with class someClass */ 
            if($(e.target).hasClass('someClass')){
                e.preventDefault();
                return;
            }
    
            /* but be carefull the contained links! to be clickable */
            if($(e.target).is('a')){
                return;
            }
    
            /* here you can code what to do when click on html */
    
        });
    
    0 讨论(0)
  • 2021-02-05 06:12

    You can also detect clicks on the whole document and check if the current element clicked is your menu element

    $(document).click(function(event){
        if(event.target !== $('.menu')[0]) {
            // hide the menu...
        }
    });​
    
    0 讨论(0)
  • 2021-02-05 06:21

    Maybe it will work like this

    $('html').click(function(e) {                    
       if(!$(e.target).hasClass('solid') )
       {
           $('.menu').remove();                
       }
    }); 
    

    see: http://jsfiddle.net/fq86U/2/

    0 讨论(0)
  • 2021-02-05 06:25

    have you tried this:

    $('.menu a').click(function(event){
       event.stopPropagation();
    });
    
    0 讨论(0)
提交回复
热议问题