Home > Article > Web Front-end > How to find the parent tag in javascript
How to find the parent tag in JS: 1. Use the parentNode attribute, the syntax is "child element object.parentNode"; 2. Use the parent() or parents() method in Jquery, the syntax is "child element object.parentNode". parent()" or "child element object.parents()".
The operating environment of this tutorial: windows7 system, javascript1.8.5&&jquery2.1.1 version, Dell G3 computer.
1. Native method--parentNode attribute
The parentNode attribute can be returned The parent node of a node.
元素.parentNode //返回元素的第一个父节点
2. Jquery method (remember to import the package)
Element.parent()
Returns the first element of the element Parent nodes
Element.parents()
Returns an array containing all parent nodes of the element
There is one below Example:
ffd43448117c1013fbeeed3f564491ad 100db36a723c770d327fc0aef2ce13b1 93f0f5c25f18dab9d176bd4f6de5d30e a80eb7cbb6fff8b0ff70bae37074b813 9c3bca370b5104690d9ef395f2c5f8d1 6c04bd5ca3fcae76e30b72ad730ca86d dc6dce4a544fdca2df29d5ac0ea9906b45a2772a6b6107b401db3c9b82c049c2975f881abd041d8a9a52113a3d7608725db79b134e9f6b82c0b36e0489ee08ed54bdf357c58b8a65c66d7c19c8e4d11416b28748ea4df4d9c2150843fecfba68 b9197df398584b98b2410e5b164be7af2cacc6d41bbb37262a98f745aa00fbf0 3f1c4e4b6b16bbbd69b2ee476dc4f83a //原生的方法 document.getElementById('a').parentNode //得到45a2772a6b6107b401db3c9b82c049c2节点 //也可以这么玩 document.getElementById('a').parentNode.parentNode //得到dc6dce4a544fdca2df29d5ac0ea9906b节点 //Jquery的方法 记得导包哦 $('#a').parent();//得到45a2772a6b6107b401db3c9b82c049c2节点 $('#a').parent().parent();//得到dc6dce4a544fdca2df29d5ac0ea9906b节点 $('#a').parents();//得到所有父级节点 45a2772a6b6107b401db3c9b82c049c2 ,dc6dce4a544fdca2df29d5ac0ea9906b ,6c04bd5ca3fcae76e30b72ad730ca86d ,100db36a723c770d327fc0aef2ce13b1 $('#a').parents('body');//得到6c04bd5ca3fcae76e30b72ad730ca86d节点 2cacc6d41bbb37262a98f745aa00fbf0 36cc49f0c466276486e50c850b7e4956 73a6ac4ed44ffec12cee46588e518a5e
[Recommended learning: javascript advanced tutorial]
The above is the detailed content of How to find the parent tag in javascript. For more information, please follow other related articles on the PHP Chinese website!