The difference between the jquery append () method and the HTML () method and the introduction of its use _jquery

Source: Internet
Author: User

Append (Content): Method inserts the specified content at the end of the selected element (still inside), and many friends feel that append is similar to HTML, and that other append in English sense are added on the original basis, while the HTML replaces all current content.

Definitions and usage

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

Use functions to attach content
Inserts the content at the end of the specified element using the function.

Grammar

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

Instance 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>

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

If the method does not set a parameter, the current contents of the selected element are returned.
return element content
When you use this method to return a value, it returns the contents of the first matching element.

Grammar

$ (selector). HTML ()

Set the contents of all P elements:

$ (". Btn1"). Click (function () { 
$ ("P"). html ("Hello <b>world</b>!"); 
});

Empty the specified element

$ ("a[href$= ' logout.asp ']"). Click (Function (event) { 
event.preventdefault (); 
$.get ("/xxlr/logout.asp", "", Function (data, textstatus) { 
if (data = 1) {//Indicates cancellation success 
$ (' #message '). HTML (""); 
$ ("#userlogin >div"). Show (); 
else { 
$ (' #message '). Append ("<p><strong> logoff failed, please retry!</strong></p>");}); 

The following is a netizen's supplement:

Of course not, append is append, HTML is completely replaced
Like <p id= "1" ><p>123</p></p>
$ ("#1"). HTML ("<span>456</span>");
The result is: <p id= "1" ><span>456</span></p>

$ ("#1"). Append ("<span></span>");
The result is: <p id= "1" ><p>123</p><span>456</span></p>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.