Differences between text()/val()/html() methods for jQuery to obtain text nodes_jquery
WBOY
Release: 2016-05-16 18:09:56
Original
1007 people have browsed it
In jquery, val, text, and html can all get values, or add a parameter to assign values. So what are the differences between them? Let’s give an example below: First of all, there are two methods in the html attribute, one has Parameter, one without parameter 1. No parameter html (): Get the html content of the first matching element. This function cannot be used with XML documents. But it can be used for XHTML documents, returning a String Example: html page code:
Hello
jquery code: $( "div").html(); Result: Hello 2. Parameter 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. Return a jquery object html page code: jquery code: $("div").html("
Nice to meet you
") ; Result: [
Nice to meet you
]
Secondly, there are two methods in the text attribute, one with parameters and one with No parameters 1. No parameters text(): Get the contents of all matching elements. The result is the text combined from the text content contained in all matching elements. What is returned is a String example: html page code:
2. Parameter text (val): Set the text content of all matching elements, with html( ) is similar, but will encode HTML (replace "<" and ">" with the corresponding HTML entities). Return a jquery object html page code:
Test Paragraph.
jquery code: $("p").text("Some new text."); Result:[
Some new text.
]
Finally, there are two methods in the val() attribute, one with parameters and one without parameters. 1. Parameterless val(): Get the current value of the first matching element. In jQuery 1.2, you can return the value of any element. Including select. If multiple selections are made, an array containing the selected values will be returned. returns a String, array Example: html page code:
] 2. Parameter val (val): Set the value of each matching element. In jQuery 1.2, this can also assign values to check, select, and radio elements and return a jquery object html page code:
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