Node.previousSibling - Web APIs 编辑
The Node.previousSibling
read-only property returns the node immediately preceding the specified one in its parent's childNodes
list, or null
if the specified node is the first in that list.
Syntax
previousNode = node.previousSibling;
Example
<img id="b0">
<img id="b1">
<img id="b2">
console.log(document.getElementById("b1").previousSibling); // <img id="b0">
console.log(document.getElementById("b2").previousSibling.id); // "b1"
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.
To navigate the opposite way through the child nodes list use Node.nextSibling.
Specifications
Specification | Status | Comment |
---|---|---|
DOM The definition of 'Node.previousSibling' in that specification. | Living Standard | No change |
Document Object Model (DOM) Level 3 Core Specification The definition of 'Node.previousSibling' in that specification. | Obsolete | No change |
Document Object Model (DOM) Level 2 Core Specification The definition of 'Node.previousSibling' in that specification. | Obsolete | No change |
Document Object Model (DOM) Level 1 Specification The definition of 'Node.previousSibling' in that specification. | Obsolete | Initial definition |
Browser compatibility
BCD tables only load in the browser
See also
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论