zl程序教程

您现在的位置是:首页 >  后端

当前栏目

Js中parentNode,parentElement,childNodes,children之间的区别

JS 区别 之间 children childNodes
2023-06-13 09:15:12 时间

parentElement获取对象层次中的父对象。 
parentNode获取文档层次中的父对象。 
childNodes获取作为指定对象直接后代的HTML元素和TextNode对象的集合。 
children获取作为对象直接后代的DHTML对象的集合。 

--------------------------------------------------------

parentNode和parentElement功能一样,childNodes和children功能一样。但是parentNode和childNodes是符合W3C标准的,可以说比较通用。而另外两个只是IE支持,不是标准,Firefox就不支持

--------------------------------------------------------

用这parentNode,childNodes2个就行了.楼上的都是正解!

--------------------------------------------------------

只是标准不一样吗?

--------------------------------------------------------

这个不是“标准不一样”,
另外两个根本就不是标准,
可以理解为ie自定义的

--------------------------------------------------------

也就是说parentElement、children是IE自家的东西,别的地方是不认的。
那么,他们的标准版就是parentNode,childNodes。
这两个的作用和parentElement、children是一样的,并且是标准的、通用的。

--------------------------------------------------------

以下是简单的解释,注意个别字的差异:
parentNodeProperty:Retrievestheparentobjectinthedocumenthierarchy.

parentElementProperty:Retrievestheparentobjectintheobjecthierarchy.

childNodes:
RetrievesacollectionofHTMLElementsandTextNodeobjectsthataredirectdescendantsofthespecifiedobject.

children:
RetrievesacollectionofDHTMLObjectsthataredirectdescendantsoftheobject.

--------------------------------------------------------

我也从dhtml手册里拿了两段^_^
parentElementchildren:
Thereisnopublicstandardthatappliestothisproperty/collection.
parentNodechildNodes:
Thisproperty/collectionisdefinedinWorldWideWebConsortium(W3C)DocumentObjectModel(DOM)Level1.

很多W3C标准从IE5才开始支持
IE4以前的只能用ie自己的方法

============================================

bbb
aaa
aaa


看见没有,这里的这个bbb,影响了结果