Home  >  Article  >  Web Front-end  >  js obtains and sets the display value of the p tag through the html() and text() methods_javascript skills

js obtains and sets the display value of the p tag through the html() and text() methods_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:48:181126browse
html() method

This method is similar to the innerHTML attribute in JavaScript and can be used to read or set the HTML content in an element. To get the content of an element, you can do this:
Copy the code The code is as follows:

var p_html = $("p").html(); //Get the HTML code of the p element

If you need to set the HTML code of a certain element, you can also use this method, but you need to set it Pass a parameter. For example, to set the HTML code of the p element, you can use the following code:
Copy the code The code is as follows:

//Set the HTML code of the p element
$("p").html("Welcome to the Concise Modern Magic Library~~");

Note: html() The method can be used with XHTML documents but not with XML documents.

text() method

This method is similar to the innerText attribute in JavaScript and can be used to read or set the text content in an element. Continue to use the above HTML code and use the text() method to operate on the p element:
Copy the code The code is as follows:

var p_text = $("p").text(); //Get the text content of the p element

Same as the html() method, if you need to set it for an element Text content, then a parameter also needs to be passed. For example, to set the text content for the p element, the code is as follows:
Copy the code The code is as follows:

/ / Set the text content of the p element
$("p").text("Welcome to the Concise Modern Magic Library~~");

Please note the following two points:

The innerText attribute in JavaScript cannot run under the Firefox browser, but jQuery's text() method supports all browsers.
The text() method is valid for both HTML documents and XML documents.

When displaying the friend list, set in the

element that displays the friend’s name:

, so that the js file can dynamically obtain the id of the

element, and successfully assign the value $("#p" userId).text(name); through the text() method.
Statement:
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