gpt4 book ai didi

javascript - jQuery append() 与 appendChild()

转载 作者:IT王子 更新时间:2023-10-29 02:48:52 25 4
gpt4 key购买 nike

下面是一些示例代码:

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() 有什么区别?
任何实时场景?

最佳答案

主要区别在于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 是安全的。

关于javascript - jQuery append() 与 appendChild(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15926325/

25 4 0
文章推荐: javascript - 在 Controller 中使用 $setValidity
文章推荐: java - 无法使用 DocumentBuilder 解析 XML 文件
文章推荐: c# - 在 xml 文件中搜索数据的最佳方式?
文章推荐: javascript - 如何避免在 React 中额外包装
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com