jQuery append() 与 appendChild() [英] jQuery append() vs appendChild()

查看:39
本文介绍了jQuery append() 与 appendChild()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一些示例代码:

function addTextNode(){var newtext = document.createTextNode(" 一些动态添加的文本.");var para = document.getElementById("p1");para.appendChild(newtext);$("#p1").append("HI");}

<p id="p1">段落的第一行.<br/></p>

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 是安全的.

Here's some sample code:

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>

What is the difference between append() and appendChild()?
Any real time scenarios?

解决方案

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 );
        }
    });
},

If you're using jQuery library on your project, you'll be safe always using append when adding elements to the page.

这篇关于jQuery append() 与 appendChild()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆