Home > Web Front-end > JS Tutorial > body text

The difference between jquery append() method and html() method and introduction to their use

巴扎黑
Release: 2017-06-24 11:22:04
Original
2120 people have browsed it

The

append() method inserts specified content at the end of the selected element, and the html() method returns or sets the content of the selected element. The following is an introduction to the difference between the two and how to use

append(content ): Method inserts specified content at the end of the selected element (still inside). Many friends think that append is similar to html. OthersIn the English sense, append is added to the original basis, while in html it is Replaces all current content.

Definition and Usage

The append() method inserts the specified content at the end of the selected element (still inside).
$(selector).append(content)

Use function to append content
Use function to insert content at the end of the specified element.

Syntax

$(selector).append(function(index,html))

Example code:


## The #

<script src="/jquery.min.js" type="text/javascript"></script> 
<style> 
.imgFocus{border: 1px solid #eee;} 
</style> 
<p> </p> 
<script type="text/javascript"> 
var showimg = "<p class=&#39;imgFocus&#39;>123456</p>"; 
$("p").append(showimg); 
</script>
Copy after login

html() method returns or sets the content (inner HTML) of the selected element.

If this method does not set parameters, it returns the current content of the selected element.

Return element content
When using this method to return a value, it will return the content of the first matching element.

Syntax

$(selector).html()

Set the content of all p elements:


$(".btn1").click(function(){ 
$("p").html("Hello <b>world</b>!"); 
});
Copy after login

Clear the specified element


$("a[href$=&#39;logout.asp&#39;]").click(function(event) { 
event.preventDefault(); 
$.get("/xxlr/Logout.asp","",function(data, textStatus) { 
if (data == 1) { //表明注销成功 
$(&#39;#message&#39;).html(""); 
$("#userlogin>p").show(); 
} 
else { 
$(&#39;#message&#39;).append("<p><strong>注销失败,请重新尝试!</strong></p>"); 
} 
}); 
});
Copy after login

The following is the netizen’s addition:

Of course it is different, append is to append, html is to completely replace

For example < ;p id="1">

123


$("#1").html("456");
The result is:

456



$("#1").append(" ");
The result is:

123

456


The above is the detailed content of The difference between jquery append() method and html() method and introduction to their use. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!