这是一些示例代码:
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()
and appendChild()
?
有实时场景吗?
主要区别在于appendChild
是一个 DOM 方法并且append
是一个 jQuery 方法。第二个使用第一个,正如您在 jQuery 源代码中看到的那样
append: function() {
return this.domManip(arguments, true, function( elem ) {
if ( this.nodeType === 1 || this.nodeType === 11 || this.nodeType === 9 ) {
this.appendChild( elem );
}
});
},
如果您在项目中使用 jQuery 库,那么始终使用是安全的append
向页面添加元素时。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)