所以这几乎是问题标题中的全部内容,您可以附加 on() 事件处理程序(用于 live() 行为)的最早点是什么?
进一步详细..
我知道ready()事件 jquery 模式,但根据一些(参考),如果您使用诸如live().
live()现在已弃用,而赞成使用on()类似的行为。
然而live(),用于$(document)在文档解析的早期附加听起来像是可用的事件。
on()另一方面,通常建议通过将事件附加到 DOM 中更接近目标的东西来使用。
因此,如果我的大部分内容都在一个#main部分中,那$('#main')将是附加on()处理程序的良好候选者。
考虑到这一点,什么时候可以安全地附加我的on()处理程序?
是不是在容器标签打开时?或者一旦关闭?
这是否可靠并且通常在浏览器中正常运行?
<html>
<!-- boring head stuff goes here (including loading jquery, obviously) -->
<body>
<header>Stuff</header>
<section id='main'>
<script>
$('#main').on('click','button.donkey', function() { alert ('you click the donkey');})
</script>
...
<button class="donkey">Donkey That!</button>
</section>
<footer>Other stuff</footer>
</body>
</html>