Compare 'e.target' to a jQuery object

后端 未结 4 1413
情书的邮戳
情书的邮戳 2021-02-06 22:00

What I want to do:

( clickedObject === someDiv ) //returns true or false

What I tried

( $(e.target) === $(\'.selector\') ); //r         


        
4条回答
  •  执笔经年
    2021-02-06 22:51

    Obviously using .is() function is the best solution here.

    If you find yourself doing such comparison, try to check if it is possible to use embedded jQuery mechanisms like this:

    $(element).on("click", ".selector", function() {
        alert("clicked");
    });
    

    Second argument in the .on() method is a target selector. When using this construction (read more: http://api.jquery.com/on/#direct-and-delegated-events) there will be no need to make any additional comparisons.

    https://jsfiddle.net/m5zysufy/

提交回复
热议问题