The HTML (), text (), Val () difference in jquery

Source: Internet
Author: User

1.HTML

HTML (): Gets the HTML content of the first matching element. This function cannot be used with XML documents. But can be used in XHTML documents

HTML (val): Sets the HTML content of each matching element. This function cannot be used with XML documents. But it can be used in XHTML documents.

2.TEXT

Text (): Gets the contents of all matching elements.

The result is text that is combined with the text content contained by all matching elements. This method is valid for both HTML and XML documents.

Text (val): Sets the textual content of all matching elements

Similar to HTML (), but encodes HTML (replaces "<" and ">" with the corresponding HTML entity).

3.VAL

Val (): Gets the current value of the first matching element.

Val (val): Sets the value of each matching element.

The above content is copied in the jquery Help document, and no more talking nonsense. Here are some of your own exercises, the code is as follows:

I found another different place in HTML and text when I was doing the exercises.

HTML () to the content of the element, you can select the element below the format is also taken.

such as: <div id= "Divshow" ><b><i>write less do more</i></b></div>

If we use var strhtml = $ ("#divShow"). html ();

The result is: <b><i>write less do more</i></b>

If we use var strHTML2 = $ ("#divShow b I"). html ();

The result is write less does more

And the text doesn't have the first case,

If we var StrText = $ ("#divShow"). Text ();

The result is write less does more

<%@ page language= "java" import= "java.util.*" pageencoding= "Utf-8"%>

<%

String path = Request.getcontextpath ();

String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";

%>

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >

<base href= "<%=basePath%>" >

<script src= "Js/jquery.js" type= "Text/javascript" ></script>

<!--

<script src= "Http://code.jquery.com/jquery-latest.js" ></script>

-

<title> Gets or sets the content of an element </title>

<style type= "Text/css" >

Body{font-size:15px;text-align:center}

Div{border:solid 0px #666;p adding:5px;width:220px;margin:5px}

</style>

<script type= "Text/javascript" >

$ (function () {

var strhtml = $ ("#divShow"). html ();//Get HTML content (contains two formats below DIV)

var strHTML2 = $ ("#divShow b I"). html (); Get HTML content

var strHTML3 = $ ("div"). html ();

var StrText = $ ("#divShow"). Text ();//Get textual content

var strText2 = $ ("div"). text ();

$ ("#divHTML"). HTML (strhtml);//Set HTML content

$ ("#divHTML2"). HTML (strHTML2); Set HTML content

$ ("#divHTML3"). HTML (strHTML3); Set HTML content

$ ("P"). html (strhtml);

$ ("#divText"). Text (strText);//Set textual content

$ ("#divText2"). Text (strText2);//Set textual content

$ ("a"). Text (StrText);

$ ("select"). Change (function () {//Set list box Change event

Gets the value of all options selected by the list box

Alert ($ ("select"). val ());

var Strsel = $ ("select"). val (). Join (",");

$ ("input"). Val (Strsel); Displays the value of all options selected by the list box

})

})

</script>

<body>

<table border= "1" bordercolor= "#A9A9A9" cellspacing= "0" >

<tr><td>******************************</td><td>*************************************** </td></tr>

<tr>

<td><div id= "Divshow" ><b><i>write less do more</i></b></div></td>

<td> This is the original content </td>

</tr>

<tr>

<td><div id= "Divshow" ><b><i>write XXXX do xxxx</i></b></div></td>

<td> This is the original content </td>

</tr>

<tr><td>******************************</td><td>*************************************** </td></tr>

<tr>

<td><div id= "divhtml" >1</div></td>

<td> get the original content (formatted with the content) and output it in HTML </td>

</tr>

<tr>

<td><div id= "DivHTML2" >2</div></td>

<td> get the original content (formatted without content) and export it as HTML </td>

</tr>

<tr>

<td><div id= "DivHTML3" >3</div></td>

<td> gets the original content (gets the content of the first match element) and outputs it as HTML </td>

</tr>

<tr>

<td><p></p></td>

<td>html way to set the text of a paragraph </td>

</tr>

<tr>

<td><p></p></td>

<td> If this also has content, it is to set the content of each matching element </td>

</tr>

<tr><td>******************************</td><td>*************************************** </td></tr>

<tr>

<td><div id= "Divtext" >4</div></td>

<td> text output After getting the original content </td>

</tr>

<tr>

<td><div id= "DivText2" ></div></td>

<td> gets the original content (gets the contents of all matching elements) and outputs it in text mode </td>

</tr>

<tr>

<td><a></a></td>

<td>text way to set the text of a paragraph </td>

</tr>

<tr>

<td><a></a></td>

<td> If this also has content, it is to set the content of each matching element </td>

</tr>

<tr><td>******************************</td><td>*************************************** </td></tr>

<tr>

<td>

<select multiple= "multiple" style= "height:96px;width:85px" >

<option value= "1" >item 1</option>

<option value= "2" >item 2</option>

<option value= "3" >item 3</option>

<option value= "4" >item 4</option>

<option value= "5" >item 5</option>

<option value= "6" >item 6</option>

</select>

<select>

<option value= "7" >item 7</option>

<option value= "8" >item 8</option>

<option value= "9" Selected>item 9</option>

</select>

</td>

<td>

</td>

</tr>

<tr>

<td><input ></input></td>

<td><input ></input></td>

</tr>

</table>

</body>

Everyone can also be verified by themselves, the above is my experiment, I use the jquery is 1.6

The HTML (), text (), Val () difference in jquery

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.