Home > Web Front-end > JS Tutorial > The difference between jquery append() method and html() method and introduction to their use_jquery

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

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-05-16 16:40:39
Original
1330 people have browsed it

append(content): This method inserts the specified content at the end of the selected element (still inside). Many friends think append is similar to html. In other words, append is added to the original in the English sense, while in html it is Replaces all current content.

Definition and usage

The

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

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

Grammar

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

Example code:

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

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 returns the contents of the first matching element.

Grammar

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

The following is what netizens added:

Of course it’s different, append means appending, html means completely replacing
For example

123


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

456



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

123

456

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
Latest Issues
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template