hellochenlu 2016-05-05 08:15 采纳率: 0%
浏览 1245

javascript 中的appendChild?

 <script type="text/javascript">
    var CreateDiv = (function(){
        var instance;

        var CreateDiv = function(html){
            if(instance){
                return instance;
            }
            this.html = html;
            this.init();
            return instance = this;
        };

        CreateDiv.prototype.init = function() {
            var div = document.createElement('div');
            div.innerHTML = this.html;
            document.body.appendChild(div);
        };

        return CreateDiv;
    })();

    var a = new CreateDiv('sven1');
    var b = new CreateDiv('sven2');

    alert(a === b);
</script>

此段代码报错,can not read property appendChild of null

  • 写回答

3条回答 默认 最新

  • LokiJ_1414 2016-05-05 08:30
    关注

    document.body 为null,可能是在body标签前就加载了js,试下把JS代码移到body标签代码结束之后

    评论

报告相同问题?