


A brief analysis of the differences between html(), text(), and val() in JQuery_jquery
1.HTML
html(): Get the html content of the first matching element. This function cannot be used with XML documents. But can be used for XHTML documents
html(val): Set the html content of each matching element. This function cannot be used with XML documents. But it can be used for XHTML documents.
2.TEXT
text(): Get the content of all matching elements.
The result is the text combined from the text content contained in all matching elements. This method works for both HTML and XML documents.
text(val): Set the text content of all matching elements
Similar to html(), but will encode HTML (replace "<" and ">" with corresponding HTML entities).
3.VAL
val(): Get the current value of the first matching element.
val(val): Set the value of each matching element.
The above content is copied from the JQuery help document, so I won’t go into too much nonsense. Here are some exercises I did, the code is as follows:
While doing the exercises, I discovered another difference between html and text
Whenhtml() removes the content of an element, the format under the selected element can also be retrieved.
For example:
If we use var strHTML = $("#divShow").html(); to get it,
The result is:Write Less Do More
If we use var strHTML2 = $("#divShow b i").html(); to get it
The result is Write Less Do More
And text does not have the first situation,
If we take var strText = $("#divShow").text();
The result is Write Less Do 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"> <html> <head> <base href="<%=basePath%>"> <script src="js/jquery.js" type="text/javascript"></script> <!-- <script src="http://code.jquery.com/jquery-latest.js"></script> --> <title> 获取或设置元素的内容</title> <style type="text/css"> body{font-size:15px;text-align:center} div{border:solid 0px #666;padding:5px;width:220px;margin:5px} </style> <script type="text/javascript"> $(function() { var strHTML = $("#divShow").html();// 获取HTML 内容(包含div下面的两个格式) var strHTML2 = $("#divShow b i").html(); //获取HTML内容 var strHTML3 = $("div").html(); var strText = $("#divShow").text();// 获取文本内容 var strText2 = $("div").text(); $("#divHTML").html(strHTML);// 设置HTML 内容 $("#divHTML2").html(strHTML2); //设置HTML内容 $("#divHTML3").html(strHTML3); //设置HTML内容 $("p").html(strHTML); $("#divText").text(strText);// 设置文本内容 $("#divText2").text(strText2);// 设置文本内容 $("a").text(strText); $("select").change(function() { // 设置列表框change 事件 // 获取列表框所选中的全部选项的值 alert($("select").val()); var strSel = $("select").val().join(","); $("input").val(strSel); // 显示列表框所选中的全部选项的值 }) }) </script> </head> <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>这是原内容</td> </tr> <tr> <td><div id="divShow"><b><i>Write XXXX Do XXXX</i></b></div></td> <td>这是原内容</td> </tr> <tr><td>******************************</td><td>*******************************************</td></tr> <tr> <td><div id="divHTML">1</div></td> <td>获取原内容(连带内容的格式)后以html方式输出</td> </tr> <tr> <td><div id="divHTML2">2</div></td> <td>获取原内容(不带内容的格式)后以html方式输出</td> </tr> <tr> <td><div id="divHTML3">3</div></td> <td>获取原内容(获取第一个匹配元素的内容)后以html方式输出</td> </tr> <tr> <td><p></p></td> <td>HTML方式设置段落的文本</td> </tr> <tr> <td><p></p></td> <td>如果这个也有内容了,就是设置每个匹配元素的内容</td> </tr> <tr><td>******************************</td><td>*******************************************</td></tr> <tr> <td><div id="divText">4</div></td> <td>获取原内容后以text方式输出</td> </tr> <tr> <td><div id="divText2"></div></td> <td>获取原内容(获取所有匹配元素的内容)后以text方式输出</td> </tr> <tr> <td><a></a></td> <td>TEXT方式设置段落的文本</td> </tr> <tr> <td><a></a></td> <td>如果这个也有内容了,就是设置每个匹配元素的内容</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> </html>
You can also verify it yourself. The above is an experiment I did. The JQuery I used is 1.6
To summarize:
.html() is used to read and modify the HTML tag of the element
.text() is used to read or modify the plain text content of the element
.val() is used to read or modify the value of a form element.
Comparison of the functions of these three methods
.html(), .text(), and .val() are all used to read the content of the selected element; only .html() is used to read the HTML content of the element (including its Html tag), .text() is used to read the plain text content of the element, including its descendant elements, and .val() is used to read the "value" value of the form element. Among them, the .and .text() methods cannot be used on form elements, and .val() can only be used on form elements; in addition, when the .html() method is used on multiple elements, only the first element is read; The .val() method is the same as .html(). If it is applied to multiple elements, only the "value" value of the first form element can be read, but .text() is different from them. If .text () When applied to multiple elements, the text content of all selected elements will be read.
.html(htmlString), .text(textString) and .val(value) are all used to replace the content of the selected element. If the three methods are used on multiple elements at the same time, they will be replaced. The contents of all selected elements.
.html(), .text(), and .val() can all use the return value of the callback function to dynamically change the content of multiple elements.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Detailed explanation of jQuery reference method: Quick start guide jQuery is a popular JavaScript library that is widely used in website development. It simplifies JavaScript programming and provides developers with rich functions and features. This article will introduce jQuery's reference method in detail and provide specific code examples to help readers get started quickly. Introducing jQuery First, we need to introduce the jQuery library into the HTML file. It can be introduced through a CDN link or downloaded

How to use PUT request method in jQuery? In jQuery, the method of sending a PUT request is similar to sending other types of requests, but you need to pay attention to some details and parameter settings. PUT requests are typically used to update resources, such as updating data in a database or updating files on the server. The following is a specific code example using the PUT request method in jQuery. First, make sure you include the jQuery library file, then you can send a PUT request via: $.ajax({u

jQuery is a fast, small, feature-rich JavaScript library widely used in front-end development. Since its release in 2006, jQuery has become one of the tools of choice for many developers, but in practical applications, it also has some advantages and disadvantages. This article will deeply analyze the advantages and disadvantages of jQuery and illustrate it with specific code examples. Advantages: 1. Concise syntax jQuery's syntax design is concise and clear, which can greatly improve the readability and writing efficiency of the code. for example,

How to remove the height attribute of an element with jQuery? In front-end development, we often encounter the need to manipulate the height attributes of elements. Sometimes, we may need to dynamically change the height of an element, and sometimes we need to remove the height attribute of an element. This article will introduce how to use jQuery to remove the height attribute of an element and provide specific code examples. Before using jQuery to operate the height attribute, we first need to understand the height attribute in CSS. The height attribute is used to set the height of an element

Title: jQuery Tips: Quickly modify the text of all a tags on the page In web development, we often need to modify and operate elements on the page. When using jQuery, sometimes you need to modify the text content of all a tags in the page at once, which can save time and energy. The following will introduce how to use jQuery to quickly modify the text of all a tags on the page, and give specific code examples. First, we need to introduce the jQuery library file and ensure that the following code is introduced into the page: <

Title: Use jQuery to modify the text content of all a tags. jQuery is a popular JavaScript library that is widely used to handle DOM operations. In web development, we often encounter the need to modify the text content of the link tag (a tag) on the page. This article will explain how to use jQuery to achieve this goal, and provide specific code examples. First, we need to introduce the jQuery library into the page. Add the following code in the HTML file:

How to tell if a jQuery element has a specific attribute? When using jQuery to operate DOM elements, you often encounter situations where you need to determine whether an element has a specific attribute. In this case, we can easily implement this function with the help of the methods provided by jQuery. The following will introduce two commonly used methods to determine whether a jQuery element has specific attributes, and attach specific code examples. Method 1: Use the attr() method and typeof operator // to determine whether the element has a specific attribute

jQuery is a popular JavaScript library that is widely used to handle DOM manipulation and event handling in web pages. In jQuery, the eq() method is used to select elements at a specified index position. The specific usage and application scenarios are as follows. In jQuery, the eq() method selects the element at a specified index position. Index positions start counting from 0, i.e. the index of the first element is 0, the index of the second element is 1, and so on. The syntax of the eq() method is as follows: $("s
