gpt4 book ai didi

javascript - 我可以用 document.getElementsByTagName ('*' 盲目地替换整个项目中所有出现的 document.all 吗?

转载 作者:行者123 更新时间:2023-11-29 18:04:53 25 4
gpt4 key购买 nike

我正在处理大量遗留代码,并且有大量 document.all 用法实例。我需要将所有这些实例(批量)替换为 Microsoft 支持的内容(据我所知,document.all 不再受支持)。

将 document.all 的所有实例替换为 document.getElementsByTagName() 是否安全?请记住 document.all 可以有多种形式,如 document.all.item("") 等。我需要知道 document.getElementsByTag('') 是否可以作为盲目替换的解决方案document.all 在任何地方使用(以任何形式)

最佳答案

来自MDN page about #document

document.all
Provides access to all elements with an id. This is a legacy, non-standard, interface and you should use the document.getElementById() method instead.

因此,document.getElementsByTagName("*") 将返回比具有 id 的元素更多的元素

这个定义意味着你可以做

document.querySelectorAll('[id]'); // NodeList

然而

MSDN has a different definition

The all collection includes one element object for each valid HTML tag. If a valid tag has a matching end tag, both tags are represented by the same element object.

在这种情况下,document.getElementsByTagName("*") 确实给出了类似的结果 (HTMLCollection)

然而,再次

NodeList 将无法重现 document.all.id_valuedocument.all[id_value] 的预期结果文档.all(foo).

HTMLCollection 确实支持 document.all.id_valuedocument.all[id_value],如 w3's DOM spec 中所述,虽然我强烈建议不要使用它而不是其他方法,例如 getElementById

实际上,您将不得不快速检查代码以确保无论您选择哪种替代方案,它都能按预期工作。

关于javascript - 我可以用 document.getElementsByTagName ('*' 盲目地替换整个项目中所有出现的 document.all 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32097476/

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