Home > Web Front-end > JS Tutorial > body text

What is the difference between text(), val() and html() in jquery

青灯夜游
Release: 2022-12-30 11:12:06
Original
3071 people have browsed it

The difference between text(), val() and html() in jquery: text() is used to access the text content of html elements; html() can not only be used to access the text content of html elements, It can also be used to access html content; val() is only used to access the content of input elements.

What is the difference between text(), val() and html() in jquery

The operating environment of this tutorial: windows7 system, jquery1.12.4 version, Dell G3 computer.

Common points: text(), html(), and val() are used to store and retrieve values ​​of html elements.

Difference:

  • text() is used to access the text content of html elements
  • html() can not only be used to access the text content of html elements, It can also be used to access html content
  • val() is used to access input element content

text() definition and usage

text() Method method sets or returns the text content of the selected element. If there are sub-tags, the text in the sub-tags is returned together, which is equivalent to the innerText of js.

The code is as follows

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <script src="./js/jquery-1.12.4.js"></script>
    <title>Document</title>
</head>
<body>
    <p id="p1">p有文本内容</p>
    <p id="p2">
        p2内的文本
        <span>span内有文本内容</span>
    </p>
    <input type="text" id="input1" value="这是一个input标签">
    <input type="text" name="" id="input2" placeholder="能成功获取">
    <button id="button1" value="这是一个button标签"></button>
   
 <script>
        console.log($("#p1").text());
        console.log($("#p2").text());
        console.log($("#p2 span").text()) ;
        console.log($("#input1").text());
        console.log($("#input2").text());
        console.log($("#button1").text());
 </script>

</body>
</html>
Copy after login

Console printed results

It can be seen that text() only outputs the text content within the label, and js The innerText method is the same as

html() definition and usage

html() method returns or sets the content of the selected element (inner HTML) , including tags. If there are sub-tags, return the sub-tag itself and the text within the sub-tag

Equivalent to innerHTML of js

If the method does not set parameters, it will return The current content of the selected element.

<body>
    <p id="p1">p有文本内容</p>
    <p id="p2">
        p2内的文本
        <span>span内有文本内容</span>
    </p>
    <input type="text" id="input1" value="这是一个input标签">
    <input type="text" name="" id="input2" placeholder="能成功获取">
    <button id="button1" value="这是一个button标签"></button>
    <script>
        console.log($("#p1").html());
        console.log($("#p2").html());
        console.log($("#p2 span").html());
        console.log($("#input1").html());
        console.log($("#input2").html());
        console.log($("#button1").html());
    </script>
</body>
Copy after login

The result of printing through the console

Print the text content in the current tag. If there is a sub-tag, combine the sub-tag itself and the Print the text together

This is similar to innerHTML of js

Notes on using text() and html():

Through the above two examples, We know that the element that exists in the document object (dom), such as p, can obtain its text value through text() and html(). Then if this element does not exist in the document object (dom), but we pass text( ) and html() to get its text value, what will happen?

The h1 element does not exist in dom, we add the following code:

console.log($(&#39;h1&#39;)) 
console.log($(&#39;h1&#39;).text()) //空字符串
console.log($(&#39;h1&#39;).html()) //undefined
Copy after login

The following is the printing result: $('h1').text() prints an empty string, $('h1' ),html() print undefined

val() definition and usage

val() method Returns or sets the value of the selected element.

The value of the element is set through the value attribute. This method is mostly used for input elements.

The method is mainly used to obtain the value of the form element

If this method does not set parameters, it returns the current value of the selected element.

<body>
    <p id="p1">p有文本内容</p>
    <p id="p2">
        p2内的文本
        <span>span内有文本内容</span>
    </p>
    <input type="text" id="input1" value="这是一个input标签1">
    <input type="text" name="" id="input2" value="这是一个input标签2" placeholder="能成功获取">
    <button id="button1" value="这是一个button标签"></button>
    <script>
        console.log($("#p1").val());
        console.log($("#p2").val());
        console.log($("#p2 span").val());
        console.log($("#input1").val());
        console.log($("#input2").val());
        console.log($("#button1").val());
    </script>
</body>
Copy after login

Printing the results through the console

val() is used to output the data in the form. It can be seen that the text in the p and span tags are not the same. It was not output. I also tested the H5 new tag placeholder

which was also not output, so this val should only be for the value attribute of the tag

So what about setting the value of val()

We add three lines of code to the script tag and set the values ​​​​of two input boxes and a botton

	$(&#39;#input1&#39;).val(&#39;123&#39;);
	$(&#39;#input2&#39;).val(&#39;123&#39;);
	$(&#39;#button1&#39;).val(&#39;123&#39;);
Copy after login

Let’s take a look at the effect and HTML structure presented by the browser:

For the input box, the value set through val() is displayed in the text box, and its own value has not been changed; for the button, the value set through val() is actually in the text box. Assigning a value to the value attribute

For more programming-related knowledge, please visit:Introduction to Programming! !

The above is the detailed content of What is the difference between text(), val() and html() in jquery. For more information, please follow other related articles on the PHP Chinese website!

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