jQuery append()vs appendChild() [英] jQuery append() vs appendChild()
本文介绍了jQuery append()vs appendChild()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下是一些示例代码:
function addTextNode(){
var newtext = document.createTextNode(" Some text added dynamically. ");
var para = document.getElementById("p1");
para.appendChild(newtext);
$("#p1").append("HI");
}
<div style="border: 1px solid red">
<p id="p1">First line of paragraph.<br /></p>
</div>
append()
之间的区别是什么?和 appendChild()
?
任何实时场景?
What is the difference between append()
and appendChild()
?
Any real time scenarios?
推荐答案
主要区别在于 appendChild
是一个DOM方法, append
是一个jQuery方法。第二个使用第一个,你可以看到jQuery源代码
The main difference is that appendChild
is a DOM method and append
is a jQuery method. The second one uses the first as you can see on jQuery source code
append: function() {
return this.domManip(arguments, true, function( elem ) {
if ( this.nodeType === 1 || this.nodeType === 11 || this.nodeType === 9 ) {
this.appendChild( elem );
}
});
},
如果您在项目中使用jQuery库,那么您将成为在向页面添加元素时始终使用追加
安全。
If you're using jQuery library on your project, you'll be safe always using append
when adding elements to the page.
这篇关于jQuery append()vs appendChild()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文