首页 > 获取body 所有的子元素,这样写为什么不行?

获取body 所有的子元素,这样写为什么不行?

<script type="text/javascript">
        countBodyChildren() {
            var body_element = document.getElementsByTagName("body")[0];
            alert(body_element.childNodes.length);
        }
        window.onload = countBodyChildren;
    </script>

es6的语法也不是这么写的...

var tmp = {
    countBodyChildren() {
        var body_element = document.getElementsByTagName("body")[0];
        alert(body_element.childNodes.length);
    }
}
tmp.countBodyChildren();

你这函数声明都没function关键字,想写成匿名函数也不是这样写的啊


别人ES6还不许恍恍惚惚哈哈哈

正经点

        你是想要所有子元素但是为什么出来个length不明白,最简单的办法:
        
        var child_arr = document.querySelectorAll('body *');
        

函数声明语句写错了,哪能这么搞

var countBodyChildren = function() {
    var body_element = document.getElementsByTagName("body")[0];
     alert(body_element.childNodes.length); };

var fun = function(){
    // ...
};

好好笑 我觉得你可以把childnodes 换成children

【热门文章】
【热门文章】