最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Error in appendchild - Stack Overflow

programmeradmin1浏览0评论

I have the following very simple code:

<html>
<head>
  <script type="text/javascript" >

    function showAlert(){
      alert("I am clicked");
    }

    function one(){
      var a1 = [1,2,3];
      for (var i=0;i<a1.length;i++) {
        var p = document.createElement('p');
        p.innerHTML = a1[i];
        p.onclick = showAlert;
        document.body.appendChild(p);
      }

      console.log("I am called");
    }

    one();   
  </script>
</head>
</html>

I am getting the following error:Uncaught TypeError: Cannot read property 'appendChild' of null. Can anybody tell me, where am I going wrong? I am testing in Chrome.

I have the following very simple code:

<html>
<head>
  <script type="text/javascript" >

    function showAlert(){
      alert("I am clicked");
    }

    function one(){
      var a1 = [1,2,3];
      for (var i=0;i<a1.length;i++) {
        var p = document.createElement('p');
        p.innerHTML = a1[i];
        p.onclick = showAlert;
        document.body.appendChild(p);
      }

      console.log("I am called");
    }

    one();   
  </script>
</head>
</html>

I am getting the following error:Uncaught TypeError: Cannot read property 'appendChild' of null. Can anybody tell me, where am I going wrong? I am testing in Chrome.

Share Improve this question edited May 20, 2014 at 16:59 animuson 54.8k28 gold badges142 silver badges150 bronze badges asked May 20, 2014 at 14:56 user2567857user2567857 4838 silver badges25 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

document.body.appendChild is run before the body is defined, hence document.body is still null.

Either move this script down under the <body></body> or delay it's execution with:

window.addEventListener("load", function () { /* we're ready */ });
document.body.appendChild(p)

In this line. Some days ago I had the same problem. This happens if you put your script before of the tag body.

<html>
<head>
<script type="text/javascript" >

    function showAlert(){
                    alert("I am clicked");
                    }


    function one(){
            var a1 = [1,2,3];
                    for (var i=0;i<a1.length;i++) {
                    var p = document.createElement('p');
                    p.innerHTML = a1[i];
                    p.onclick = showAlert;
                    document.body.appendChild(p);
                    }

            console.log("I am called");
            }

            one();


     </script>
 </head>
 </html>

Will not work. If you put your function call after the tag it works.

<html>
 <head>
    <script>

    function showAlert(){
                    alert("I am clicked");
                    }


    function one(){
            var a1 = [1,2,3];
                    for (var i=0;i<a1.length;i++) {
                    var p = document.createElement('p');
                    p.innerHTML = a1[i];
                    p.onclick = showAlert;
                    document.body.appendChild(p);
                    }

            console.log("I am called");
            }

    </script>
 </head>
 <body>

 <body>
<script type="text/javascript" >
            one();
     </script>
 </html>

In your context, "body" yet didn't be initialized, so it is null

It looks like you are missing the body tag from your HTML. So document.body is null

发布评论

评论列表(0)

  1. 暂无评论