如何获取父节点的上一兄弟节点

2016-01-28 20:52:16 +08:00
 just1

找找文档,应该是 this.parentNode.previousSibling
但是返回的不是对象而是 text...
怎么获取父节点的上一兄弟节点呢

2586 次点击
所在节点    JavaScript
8 条回复
just1
2016-01-28 21:01:10 +08:00
0
Keita1314
2016-01-28 21:12:31 +08:00
previousElementSibling 而不是 previousSibling
Keita1314
2016-01-28 21:16:52 +08:00
Notes
Gecko-based browsers insert text nodes into a document to represent whitespace in the source markup. Therefore a node obtained, for example, using Node.firstChild or Node.previousSibling may refer to a whitespace text node rather than the actual element the author intended to get.

See Whitespace in the DOM and W3C DOM 3 FAQ: Why are some Text nodes empty? for more information.
just1
2016-01-28 21:29:16 +08:00
@Keita1314 非常感谢
Roycom
2016-01-28 22:09:37 +08:00
mark
pysama
2016-01-28 22:34:56 +08:00
@just1
对于不支持 previousElementSibling 的浏览器。循环获取 previousSibling ,直到其 nodeType 为 1
westpsk
2016-01-28 22:38:12 +08:00
我只想问为什么要用裴博士作为头像
just1
2016-01-29 23:24:20 +08:00
@westpsk 当初要传头像,刚好这张图片

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/254083

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX