search

Home  >  Q&A  >  body text

javascript - How to write jquery to exclude an element and its sub-elements and select all other elements

As shown in the picture, only clicking on the yellow and blue areas will execute the function. Clicking on the red area and the green sub-elements within the red will not execute the function. How to write it with jquery?

伊谢尔伦伊谢尔伦2767 days ago789

reply all(6)I'll reply

  • 淡淡烟草味

    淡淡烟草味2017-05-19 10:46:06

    $('#yellow','#blue').click(function(){
        // dosomething...
    })

    reply
    0
  • 世界只因有你

    世界只因有你2017-05-19 10:46:06

    :not()

    http://www.w3school.com.cn/cs...

    reply
    0
  • 为情所困

    为情所困2017-05-19 10:46:06

    var _sel = true;
    $(".red",".green").click(function{
    _sel = false;
    })
    $('.yellow','.blue').click(function( ){
    _sel = true;
    })
    if(_sel == true){
    //Execute function
    }
    or

    There is a public class to execute the function
    $(class).click(funciton(){

        //执行函数

    })

    Red and green do not have this class

    reply
    0
  • 迷茫

    迷茫2017-05-19 10:46:06

    Finally, I first wrote a function that is executed when all elements are clicked. In the function, it is judged whether the className of the clicked element and the parent element of the element have the className. If it is true, the following content will not be executed

    $(document).click(function(){
      if(!(event.target.className==="red"||event.target.parents().hasClass('red')){
        //执行相关操作
      }
    }

    reply
    0
  • 阿神

    阿神2017-05-19 10:46:06

    Event bubbling, just event.target

    reply
    0
  • PHPz

    PHPz2017-05-19 10:46:06

    There are siblings that may be able to solve this...

    reply
    0
  • Cancelreply