Differences between jquery append () and html () methods, jqueryappend

Source: Internet
Author: User

Differences between jquery append () and html () methods, jqueryappend
Append (content): The method inserts the specified content at the end of the selected element (still inside). Many friends think that append is similar to html, in other words, append is added based on the original content, while in html, it replaces all the current content.
Definition and usage
The append () method inserts the specified content at the end of the selected element (still inside.
$ (Selector). append (content)
Add content using functions
Use a function to insert content at the end of a specified element.
Syntax

$ (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 no parameter is set for this method, the current content of the selected element is returned.
Returned Element Content
When this method is used to return a value, it returns the content of the First Matching Element.

Syntax

Optional (selector).html ()


Set the content of all p elements:

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

Clear in specified Element

$ ("A [href $ = 'logout. asp '] "). click (function (event) {event. preventDefault (); $. get ("/xxlr/Logout. asp "," ", function (data, textStatus) {if (data = 1) {// indicates that the logout is successful. ('your message'}.html (""); $ ("# userlogin> div "). show ();} else {$ ('# message '). append ("<p> <strong> logout failed. Please try again! </Strong> </p> ");}});});



In jquery, how does one use the append () method to add new elements? $ ('# Id') cannot be obtained in html?

. Append (content)
A simple example
<! DOCTYPE html>
<Html>
<Head>
<Style>
P {background: yellow ;}
</Style>
<Script src = "code.jquery.com/jquery-1.4.4.js"> </script>
</Head>
<Body>
<P> I wowould like to say: </p>
<Script>
$ ("P"). append ("<strong> Hello </strong> ");
</Script>

</Body>
</Html>

========================================================== =
I tried it locally to get the value.
<Div id = "con"> </div>
<Div id = "result"> </div>
<Script>
$ ("# Con"). append ("<div id = 'hello'> hello </div> ");
$ ("# Result" ).html ($ ("# hello" ).html ());
</Script>

You can see that there are two Hello

Use the append () method of jQuery to append the html, and use the selector to obtain the appended element.

For example, in the following example, the appended element is directly added as a string.
$ ('Body'). append ('<div class = "clickme"> Another target </div> ');
You can declare a character and obtain the element to be appended.

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.