简体   繁体   English

appendChild + createElement

[英]appendChild + createElement

What's the difference between: 有什么区别:

var div = document.createElement('div');//output -> [object HTMLDivElement]

document.getElementById('container').appendChild(div);

and: 和:

var div = '<div></div>';

document.getElementById('container').appendChild(div);//output -> <div></div>

Shouldn't both be the same? 两者不应该是一样的吗? And if not, how do I get the second version to work? 如果没有,我如何让第二个版本工作?

The latter is simply a string containing HTML while the first is an object. 后者只是一个包含HTML的字符串,而第一个是一个对象。 For the first, you need appendChild while for the second, you need to append to innerHTML . 首先,你需要appendChild而对于第二个,你需要附加到innerHTML

shouldn't both be the same? 两者不应该是一样的吗? and if not, how do i get the 2nd version to work? 如果没有,我如何让第二个版本工作?

var div = '<div></div>';
document.getElementById('container').innerHTML += div;

With your JS/DOM engine, calling Element.appendChild with a string as an argument causes a new Text node to be created then added. 使用JS / DOM引擎,使用string作为参数调用Element.appendChild会导致创建新的Text节点,然后添加。

Your first example creates a <div> element. 您的第一个示例创建了一个<div>元素。 Your second example creates a text node with <div></div> as its contents. 第二个示例创建一个文本节点,其中包含<div></div>作为其内容。

Your second example is equivalent to: 你的第二个例子相当于:

var div = '<div></div>';

document.getElementById('container').appendChild(document.createTextNode(div));//output -> <div></div>

As Sarfraz Ahmed mentioned in his answer , you can make the second example work how you want it to work by writing: 正如Sarfraz Ahmed在他的回答中提到的那样 ,你可以通过编写第二个例子来实现你希望它如何工作:

var div = '<div></div>';

document.getElementById('container').innerHTML = div;

appendChild really does expect a HTMLDomNode of some kind, such as a HTMLDivElement , and not a string. appendChild确实需要某种HTMLDomNode,例如HTMLDivElement ,而不是字符串。 It doesn't know how to deal with a string. 它不知道如何处理字符串。 You could do 你可以做到

document.getElementById('container').innerHTML += div;

but I really prefer the first version; 但我真的更喜欢第一个版本; and I'd rely more on it to behave the same across browsers. 而且我更依赖它来跨浏览器表现相同。

appendChild is expecting an element so when you send it text, it doesn't know what to do. appendChild期待一个元素,所以当你发送文本时,它不知道该怎么做。 You might want to look into using a javascript library to ease some of that work, such as jQuery. 您可能希望使用javascript库来简化某些工作,例如jQuery。 Your code would be: 你的代码是:

$('#container').append('<div></div>');

The simplest solution and support all browsers is: 最简单的解决方案并支持所有浏览器:

var div = '<div></div>';
var parser = new DOMParser();
var myDiv = parser.parseFromString( html, "text/xml" );

Another solution may be: 另一个解决方案可能是

var div = '<div></div>';
var tmpDiv = document.createElement('div');
    tmpDiv.innerHTML = div;

    elDiv = tmpDiv.childNodes[0]; //Now it can be used as an element

You can also use these to append/prepend an element to the DOM respectively: 您还可以使用它们分别将元素附加/前置到DOM:

var elem = document.documentElement.appendChild(document.createElement(element));
var elem = document.documentElement.prepend(document.createElement(element));

and use the elem variable to target the element (eg): 并使用elem变量来定位元素(例如):

elem.style.display = "block";
elem.style.remove();
elem.style.id = ...;

etc. 等等

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM