[英]How to add element inside the div
Let I've created an element as the following: 让我创建一个如下元素:
var elem= document.createElement("p");
And I've a div#parent
element. 我有一个
div#parent
元素。 How can I put elem inside the div#parent
? 如何将elem放入
div#parent
? Is it possible to do without jQuery
? 是否可以不使用
jQuery
?
You have to use appendChild method to add / append a child. 您必须使用appendChild方法添加/附加子项。
document.getElementById('parent').appendChild( document.createElement("p") );
More Info: 更多信息:
Nope, have to use jQuery. 不,必须使用jQuery。 That's your only option.
那是您唯一的选择。 :)
:)
Just kidding. 开玩笑。
Something like this should work: 这样的事情应该起作用:
var parent = document.getElementById('parent');
parent.appendChild(elem);
It's that simple. 就这么简单。 Even without using good 'ole jQuery!
即使没有使用好的'ole jQuery!
Yes, you can use appendChild()
: 是的,您可以使用
appendChild()
:
var parent = document.getElementById('parent');
var elem= document.createElement("p");
parent.appendChild(elem);
Of course you can! 当然可以!
var parent = document.querySelector("div#parent");
//or document.getElementById("parent");
//I doubt speed is an issue so both will work the same
parent.appendChild(elem);
By appendChild()
通过
appendChild()
var elem = document.createElement("p");
var div = document.getElementById("parent");
div.appendChild(elem);
And to alaborate. 并详细说明。 jQuery is written in pure Javascript.
jQuery用纯Java脚本编写。 Anything jQuery do, you can do.
jQuery可以做的任何事情。 That said, there are various quirks etc. that it abstract away.
就是说,它有各种抽象的怪癖等。
var parent = document.getElementById("parent");
parent.appendChild(elem);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.