gpt4 book ai didi

jquery - 如果我覆盖html,它会从dom中删除吗?

转载 作者:行者123 更新时间:2023-12-03 22:48:06 25 4
gpt4 key购买 nike

假设我有以下代码:

...
<div id="div1">
<div id="div2">Original div2</div>
</div>

<div id="div3"></div>

...

如果我运行

$('#div1').html('');
$('#div3').html('<div id="div2">New div2</div>');

我最终会遇到问题吗,因为我没有使用 .remove() 从 dom 中删除 #div2,或者以这种方式清除 html 是否可以为我做到这一点?

如果 div2 包含一些附加处理程序的 javascript 会怎样,比如

$('#div2').on('click',function() { ... });

这也会被删除,还是我需要关闭()它?

最佳答案

do I end up with problems because I didn't use .remove() to remove #div2 from the dom, or does clearing the html in this way do that for me?

不,jQuery 将在内部为您使用 .remove (从技术上讲,它使用 cleanData,但它执行相同的清理操作。)

事件将会丢失,因此您必须重新绑定(bind)它们。

关于jquery - 如果我覆盖html,它会从dom中删除吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16044650/

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