Is binding a click event to document better than binding it to body?

后端 未结 5 1974
逝去的感伤
逝去的感伤 2021-02-20 15:55

The question is simply between

$(\"body\").click(function(e){}); vs $(document).click(function(e){});

which is more efficient or advisa

5条回答
  •  猫巷女王i
    2021-02-20 16:33

    I'd treat this similarly to anything else in the DOM.

    If I bind to a

    then it's at a higher level than the inside it. If I bind to the then it's lower than the
    . This works the same way for the document(higher) and the (lower) simply a level issue in my opinion.

    So if you want to ensure that you're binding the click event to the highest element then bind to the document.

    提交回复
    热议问题