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.

句法

previousNode = node.previousSibling;
					

范例

<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"
					

注意事项

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.

Whitespace in the DOM and W3C DOM 3 FAQ: Why are some Text nodes empty? 了解更多信息。

To navigate the opposite way through the child nodes list use Node.nextSibling .

规范

规范 状态 注释
DOM
The definition of 'Node.previousSibling' in that specification.
实时标准 无变化
DOM (文档对象模型) 3 级核心规范
The definition of 'Node.previousSibling' in that specification.
过时 无变化
DOM (文档对象模型) 级别 2 核心规范
The definition of 'Node.previousSibling' in that specification.
过时 无变化
DOM (文档对象模型) 1 级规范
The definition of 'Node.previousSibling' in that specification.
过时 初始定义

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
previousSibling Chrome Yes Edge 12 Firefox Yes IE 5.5 Opera Yes Safari Yes WebView Android Yes Chrome Android Yes Firefox Android Yes Opera Android Yes Safari iOS Yes Samsung Internet Android Yes

图例

完整支持

完整支持

另请参阅

元数据

  • 最后修改: