gpt4 book ai didi

javascript - 为什么需要 jquery 的 .data() 方法

转载 作者:行者123 更新时间:2023-11-30 13:16:49 25 4
gpt4 key购买 nike

在下面的 getElementsByTagName("p")[0]getElementById("demo") 中访问相同的元素。

以下两项工作,所以我无法弄清楚为什么甚至需要 jquery 数据功能。第二个是不能移植到所有浏览器。

$(document.getElementsByTagName("p")[0]).data("funcZ", function() {console.log("ZZZZZ")})

$(document.getElementById("demo")).data("funcZ")()

document.getElementsByTagName("p")[0].funcX = function() {console.log("XXXXX")}

document.getElementById("demo").funcX()

最佳答案

根据 jQuery 网站:

The jQuery.data() method allows us to attach data of any type to DOM elements in a way that is safe from circular references and therefore free from memory leaks. jQuery ensures that the data is removed when DOM elements are removed via jQuery methods, and when the user leaves the page.

有可能通过将随机字段附加到 DOM 元素,当 DOM 元素消失时,这些字段保留在内存中。看起来 jQuery 会为您处理。

关于javascript - 为什么需要 jquery 的 .data() 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11730699/

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