2009-12-14 60 views
2

使用jQuery,什么是使用之间的性能差异:.empty()。append()和.html()之间有什么区别?

$('#somDiv').empty().append('text To Insert') 

$('#somDiv').html('text To Insert') 

+2

您继续根据答案更改您的问题。你真实*最终的问题是什么? 正如你所写,没有功能差异。 如果你关心速度,你应该在你的问题上谈论速度。 – 2009-12-14 20:06:12

+8

-1用于更改根源问题并使多个答案无效。 – 2009-12-14 20:33:45

+4

-1备份斯蒂芬,因为我不在乎我是否输了一分。 – 2009-12-14 20:38:25

回答

6

.html将覆盖DIV的内容。

.append将添加到DIV的内容。

+0

是否附加使用DOM?而innerHTML不? – AppleTrue 2009-12-14 19:53:49

+2

请注意,我已经包含了empty()函数,那个append就像.html一样被覆盖。 .append比.html更快吗? – AppleTrue 2009-12-14 20:02:19

+2

不回答OP的问题...... – James 2009-12-14 20:31:16

0

在简单的话:

$('#somDiv').append('blabla') 

是这样工作的:

<div id='somDiv'>some text</div> 

变为:

<div id='somDiv'>some textblabla</div> 

而且innerHTML的替代内容,因此它变成这样:

<div id='somDiv'>blabla</div> 
+0

我已经更新了我的原始文章以包含empty()函数,以便它们的行为相同 – AppleTrue 2009-12-14 19:55:25

+0

它们在结果方面完全相同,但是对于大文件,innerHTML可能会更快,因为它仅使用本机函数只有1个电话。 – EarthMind 2009-12-14 19:58:30

+0

.append比.html更快吗? – AppleTrue 2009-12-14 19:59:04

38

$('#somDiv').html(value)相当于$('#somDiv').empty().append(value)

来源:jQuery source

+0

我已更新,使其.html()。现在有什么区别? – AppleTrue 2009-12-14 19:57:28

+0

@AppleTrue:更新的答案 – 2009-12-14 19:58:48

+0

.append比.html更快吗? – AppleTrue 2009-12-14 20:01:05

0

正确的语法是

$("#somDiv").html("<span>Hello world</span>"); 
+1

我已更新我的原始帖子。现在有什么区别? (谢谢) – AppleTrue 2009-12-14 19:58:04

+0

功能上,现在没有区别。 – 2009-12-14 20:32:51

0
difference between append() and html() in jquery 

.aapend() and .html() is the most usefull method in jQuery.But these are far different from one another, .append() add some value with the existing one.whether .html() do the same but it removes the old value 
Here is an example is given 

<ul id="test"> 
<li>test</li> 
</ul> 

Now I will use .append() to add one <li>, For that I will write 
<script type="text/javascript>" 
jQuery("#test").append("<li>test1</li>"); 
</script> 

The output of this jQuery will be 
<ul id="test"> 
<li>test</li> 
<li>test1</li> 
</ul> 

Now if I use .html() to add one <li>, For that I will write 
<script type="text/javascript>" 
jQuery("#test").html("<li>test1</li>"); 
</script> 

The output of this Script will be 
<ul id="test"> 
<li>test1</li> 
</ul> 

Here in this example .append() add one extra <li>, whether .html() removes the old one with new one. This is the main difference between .append() and .html() in Jquery `enter code here` 
0

理解jQuery中提供的扩展的意义和功能。

$('#somDiv').empty().append('text To Insert') 

- 上面的代码将清除DIV ID标签内容第一,然后将追加文本的目标DIV:

$('#somDiv').html('text To Insert') 

- 上面的代码替换HTML文本div标签的文字:

相关问题