Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

遍历DOM节点的正确方法 #68

Closed
Dream4ever opened this issue May 13, 2019 · 0 comments
Closed

遍历DOM节点的正确方法 #68

Dream4ever opened this issue May 13, 2019 · 0 comments
Labels
Front-end Everything you see and experience JS Javascript

Comments

@Dream4ever
Copy link
Owner

正文

document.querySelectorAll 获取到一批 DOM 节点之后,常常需要遍历这些节点,为每个节点绑定对应的事件。

JavaScript closure inside loops – simple practical example 中虽然讲到可以用 forEachlet,但是在部门的 iPhone 4、三星平板这两部旧设备上,无法使用这些新特性。

上网查了一下,在 Using forEach on an array from getElementsByClassName results in “TypeError: undefined is not a function” 中讲到,用 document.querySelectorAll 之类的方法获取到的 DOM 节点,是 HTMLCollection,是“类数组对象”,所以在一些旧设备上,是无法使用 forEach 这样的方法来遍历 DOM 节点的。

最保险的方法,就是采用最原始的 for 循环来遍历这些 DOM 节点,绝对不会出错。

参考资料

@Dream4ever Dream4ever added Front-end Everything you see and experience JS Javascript labels May 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Front-end Everything you see and experience JS Javascript
Projects
None yet
Development

No branches or pull requests

1 participant