search

Home  >  Q&A  >  body text

How to implement a browser-compatible version of element.children?

<body>
<p id="ap">
<h1>name</h1><p>contentcontentcontent<span>content</span></p></p>
<script>
var someElement={
    children:function(element){
        if(element.children){
            console.log(element.children);
        } else {
            var i,
                len=element.childNodes.length;
            for(i=0;i<len;i++){
                if(element.childNodes[i].nodeTypoe == 1){
                    element.childNodes[i];//怎样把得到的元素节点放在一起,之前搜过,说是放在数组里,可那个不是HTMLCollection啊。
                }
            }
        }
    }
};
var p = document.getElementById("ap");
someElement.children(p);
</script>
</body>
PHP中文网PHP中文网2777 days ago481

reply all(1)I'll reply

  • 仅有的幸福

    仅有的幸福2017-05-19 10:24:40

    To shield the compatibility issues of browser DOM operations, jquery is still the first choice

    reply
    0
  • Cancelreply