The difference between HTML and text methods in jqueryweb Design and development /byBruce
Blackened jquery time is not long, used to write directly with JS, now found in the jquery framework is very convenient to use, not only the code is less, the use is relatively simple, for the compatibility of the browser does not have to worry about the use of the process will also encounter some questions, The difference between the method HTML () and text () that is used when attaching a child tag in an HTML tag.
Usually when writing Ajax with jquery, the HTML () method is used instead of the text (), what is the difference between them?
First, there are two methods in the HTML attribute, one with a parameter and one without a parameter. Below we all use the example to look, this is easier to help understand.
1. No parameter HTML (): Gets the HTML content of the first matching element. This function cannot be used with XML documents. But it can be used in an XHTML document, which returns a string
Example:
HTML page Code:<div><span>hello</span></div>
jquery Code: $ ("div"). html ();
Result:<span>hello</span>
2. Parameter HTML (val): Sets the HTML content for each matching element. This function cannot be used with XML documents. But it can be used in XHTML documents. Returns a JQuery object
HTML page Code:<div></div>
jquery Code: $ ("div"). html ("<p>nice to meet you</p>");
Results:<div><p> Nice to meet you</p></div> (HTML tags will be recognized by the browser)
Second, there are two methods in the Text property, one with a parameter, one without a parameter
1. No parameter text (): Gets the contents of all matching elements. The result is text that is combined with the text content contained by all matching elements. The return is a string.
Example:
HTML page Code:<p><span>hello<span><div> fine</div></p>
jquery Code: $ ("P"). text ();
Result: Hello
Fine
2. Parameter text (val): Sets the text content of all matching elements, similar to HTML (), but encodes the HTML (replaces "<" and ">" with the corresponding HTML entity). Returns a JQuery object.
HTML page Code:<div>paragraph.</div>
jquery Code: $ ("div"). Text ("<b>Some</b> new text.");
Result: <p><b>Some</b> new text.</p> (HTML tags are not recognized by the browser, but are output as strings)
As we know from the above example, the difference between the HTML () method and the text () method is that it differs in the recognition of HTML tags.
Tags: jquery
The difference between HTML and text methods in jquery