将事件绑定在正文或文档上? [英] bind events on body or document?
本文介绍了将事件绑定在正文或文档上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有时用户将事件绑定在$('body')
上,有时还绑定在$(document)
sometimes the users bind the events on $('body')
and sometimes on $(document)
$(document).on('click', someAction);
$('body').on('click', someAction);
有什么理由比另一个更喜欢吗?
Is there some reason to prefer one to another?
推荐答案
对我来说,主要有一个原因将事件绑定到$(document)
而不是不绑定到$('body')
:
For me, there is mainly one reason to bind the events on $(document)
and not to $('body')
:
无需等待 domReady (文档在所有内容都可用之前可用) 其他)
no need to wait domReady (document is available before everything else)
这篇关于将事件绑定在正文或文档上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文