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

Q24. firstChild和firstElementChild的区别 #27

Open
lstoryc opened this issue Jul 24, 2020 · 1 comment
Open

Q24. firstChild和firstElementChild的区别 #27

lstoryc opened this issue Jul 24, 2020 · 1 comment

Comments

@lstoryc
Copy link
Owner

lstoryc commented Jul 24, 2020

firstChild: https://developer.mozilla.org/zh-CN/docs/Web/API/Node/firstChild

firstElementChild: https://developer.mozilla.org/zh-CN/docs/Web/API/ParentNode/firstElementChild

@lstoryc
Copy link
Owner Author

lstoryc commented Aug 3, 2020

firstChild:

Gecko内核(**Firefox**)的浏览器会在源代码中标签内部有空白符的地方插入一个文本结点到文档中.因此,使用诸如 Node.firstChild 和 Node.previousSibling 之类的方法可能会引用到一个空白符文本节点, 而不是使用者所预期得到的节点.

所以获取某元素的第一个子元素,我们应该用 firstElementChild,但是要注意浏览器兼容问题(IE8、IE9 和 Safari 需要添加 Polyfill)

@lstoryc lstoryc closed this as completed Aug 3, 2020
@lstoryc lstoryc reopened this Aug 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant