javascript 与 div 元素一起使用时未定义 appendChild 方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11514131/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 13:28:46  来源:igfitidea点击:

appendChild method is not defined when using with a div element

javascriptdomappend

提问by Rafael Adel

I'm really a beginner in Javascript and trying to add a pelement inside a divelement in this HTML :

我真的是 Javascript 的初学者,并试图在此 HTML的p元素内添加一个元素div

<!doctype html>
<html>
    <head>
    </head>

    <body>
        <p>This is paragraph 1.</p>
        <p>This is paragraph 2.</p>
        <p>This is paragraph 3.</p>
        <p>This is paragraph 4.</p>
        <div>
            <p id="foo">This is paragraph 5.</p>
        </div>
        <script type="text/javascript" src="script.js"></script>
    </body>

</html>

using this code :

使用此代码:

(function(){
    var divElement = document.getElementsByTagName("div");
        el = document.createElement("p");
        content = document.createTextNode("This is text");    
    el.appendChild(content);
    document.divElement.appendChild(el);        
}());

But i get this error at line 6 :

但我在第 6 行收到此错误:

Uncaught TypeError: Cannot call method 'appendChild' of undefined

未捕获的类型错误:无法调用未定义的方法“appendChild”

回答by xandercoded

getElementsByTagName(" ... ")returns a collection of elements.

getElementsByTagName(" ... ")返回元素的集合。

var divElement = document.getElementsByTagName("div")[0];

Also, change:

另外,更改:

document.divElement.appendChild(el); 

To:

到:

divElement.appendChild(el);

回答by Blender

One of your variable names is a bit misleading, namely divElement. Since you are using getElementsByTagName()(notice elements), it will return a list of elements, not a single one. Therefore, you should name the variable divElementsto make it less confusing.

您的变量名称之一有点误导,即divElement. 由于您正在使用getElementsByTagName()(notice elements),它将返回一个元素列表,而不是单个元素。因此,您应该命名变量divElements以减少混淆。

I've modified your code to work with a list of elements instead:

我已经修改了您的代码以使用元素列表:

(function() {
    var divElements = document.getElementsByTagName("div");
    var el = document.createElement("p");
    var content = document.createTextNode("This is text");

    el.appendChild(content);

    for (var i = 0; i < divElements.length; i++) {
        divElements[i].appendChild(el);
    }
}());?

And a demo: http://jsfiddle.net/UmKYq/5/

还有一个演示:http: //jsfiddle.net/UmKYq/5/

回答by Lavee Gorakhpuriya

var div =  document.createElement("div");

document.lastChild.appendChild(div);

var a=[];

var n=[];



var txt=["Lovey", "komal", "Sona", "Purvi", "Sanchi"];

for (i=0; i<=txt.length; i++)

{
    a[i]=document.createElement("a");

    a[i].setAttribute("href","#");

    n[i].appendChild(n[i]);

    div.appendChild(n[i]);

    div.appendChild(a[i]);   

}