gpt4 book ai didi

jquery - $(document).on() 和 $(element).on() 有什么不同

转载 作者:行者123 更新时间:2023-12-03 22:13:45 26 4
gpt4 key购买 nike

我知道 jquery .on() 的用途和目的,因为我使用它。

但我想知道这个脚本中 $(document).on()$(element).on() 之间有什么区别:

<html>
...
<body>
...
<table id="table-user">
<thead>...</thead>
<tbody>
AJAX DYNAMIC CONTENT
</tbody>
</table>
....
<script>
$(document).on('click','.btn-edit',function(){
go_edit();
});

$('#table-user').on('click','.btn-edit',function(){
go_edit();
});

</script>
</body>
</html>

它们之间是否存在性能差异或其他问题?

最佳答案

$(document).on('click','.btn-edit',function()

这会将单击事件绑定(bind)到文档及其中的所有子元素。此方法称为委托(delegate)事件处理。

$('#table-user').on('click','.btn-edit',function()

直接将点击事件绑定(bind)到#table-user。直接在元素上捕获事件。

关于jquery - $(document).on() 和 $(element).on() 有什么不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41820906/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com