admin管理员组文章数量:1026989
Both Visual Studio Code and the Mozilla Web Docs tell me that document.all
is deprecated and should not be used in new websites, like mine.
However, document.all
seemed to serve a very important purpose: getting a list of every node and their children in a document (either for manipulating the current HTML page, or when parsing XML file or string).
I've seen a lot of advice that suggests document.getElementById
instead. But that only solves the problem of using document.all
with indices to fetch one element (which is pretty obviously bad practice.) This in no way offers a solution for simply getting a list of every element and their children in a DOM, especially for iterating.
What is the proper, modern replacement for this method?
Both Visual Studio Code and the Mozilla Web Docs tell me that document.all
is deprecated and should not be used in new websites, like mine.
However, document.all
seemed to serve a very important purpose: getting a list of every node and their children in a document (either for manipulating the current HTML page, or when parsing XML file or string).
I've seen a lot of advice that suggests document.getElementById
instead. But that only solves the problem of using document.all
with indices to fetch one element (which is pretty obviously bad practice.) This in no way offers a solution for simply getting a list of every element and their children in a DOM, especially for iterating.
What is the proper, modern replacement for this method?
Share Improve this question asked May 26, 2021 at 16:33 Nat RiddleNat Riddle 1,0101 gold badge14 silver badges28 bronze badges 11-
4
how about
document.querySelectorAll("*")
? – Nicholas Tower Commented May 26, 2021 at 16:35 -
3
document.querySelectorAll("*")
returns exact same # of elements asdocument.all
on this page. – spender Commented May 26, 2021 at 16:38 - 1 @NatRiddle But also iterating to find multiple elements would be weird. Usually you'd parse your XML using nested loops, possibly recursion, starting from the root node. – Bergi Commented May 26, 2021 at 17:00
- 1 @NatRiddle The problem with not using a recursive function means that you can't really distinguish the recursive structure though – Bergi Commented May 26, 2021 at 17:11
-
1
@NatRiddle There's also a
TreeWalker
you might want to look at. But recursive functions are the more natural approach to processing recursive structures imo. If you ask a new question about the actual data processing you want to do, with an example of your xml, we might be able to suggest a more concrete solution – Bergi Commented May 26, 2021 at 17:30
2 Answers
Reset to default 6A solution for html would be document.querySelectorAll("*")
What is the proper, modern replacement for this method, especially for iterating?
A NodeIterator
.
Both Visual Studio Code and the Mozilla Web Docs tell me that document.all
is deprecated and should not be used in new websites, like mine.
However, document.all
seemed to serve a very important purpose: getting a list of every node and their children in a document (either for manipulating the current HTML page, or when parsing XML file or string).
I've seen a lot of advice that suggests document.getElementById
instead. But that only solves the problem of using document.all
with indices to fetch one element (which is pretty obviously bad practice.) This in no way offers a solution for simply getting a list of every element and their children in a DOM, especially for iterating.
What is the proper, modern replacement for this method?
Both Visual Studio Code and the Mozilla Web Docs tell me that document.all
is deprecated and should not be used in new websites, like mine.
However, document.all
seemed to serve a very important purpose: getting a list of every node and their children in a document (either for manipulating the current HTML page, or when parsing XML file or string).
I've seen a lot of advice that suggests document.getElementById
instead. But that only solves the problem of using document.all
with indices to fetch one element (which is pretty obviously bad practice.) This in no way offers a solution for simply getting a list of every element and their children in a DOM, especially for iterating.
What is the proper, modern replacement for this method?
Share Improve this question asked May 26, 2021 at 16:33 Nat RiddleNat Riddle 1,0101 gold badge14 silver badges28 bronze badges 11-
4
how about
document.querySelectorAll("*")
? – Nicholas Tower Commented May 26, 2021 at 16:35 -
3
document.querySelectorAll("*")
returns exact same # of elements asdocument.all
on this page. – spender Commented May 26, 2021 at 16:38 - 1 @NatRiddle But also iterating to find multiple elements would be weird. Usually you'd parse your XML using nested loops, possibly recursion, starting from the root node. – Bergi Commented May 26, 2021 at 17:00
- 1 @NatRiddle The problem with not using a recursive function means that you can't really distinguish the recursive structure though – Bergi Commented May 26, 2021 at 17:11
-
1
@NatRiddle There's also a
TreeWalker
you might want to look at. But recursive functions are the more natural approach to processing recursive structures imo. If you ask a new question about the actual data processing you want to do, with an example of your xml, we might be able to suggest a more concrete solution – Bergi Commented May 26, 2021 at 17:30
2 Answers
Reset to default 6A solution for html would be document.querySelectorAll("*")
What is the proper, modern replacement for this method, especially for iterating?
A NodeIterator
.
本文标签: domReplacement for documentall JavaScriptStack Overflow
版权声明:本文标题:dom - Replacement for document.all JavaScript - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745666095a2162185.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论