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

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

不言
Release: 2018-05-07 16:09:54
Original
3477 people have browsed it

This article mainly introduces js to obtain and set the display value of the p tag through the html() and text() methods. Friends who need it can refer to the

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 code The code is as follows:

var p_html = $("p").html(); //获取p元素的HTML代码
Copy after login

If you need to set the HTML code of an element, you can also Use this method, but you need to pass it a parameter. For example, to set the HTML code of the p element, you can use the following code:

Copy code The code is as follows:

//设置p元素的HTML代码 
$("p").html("欢迎您访问简明现代魔法图书馆~~");
Copy after login

Note: The html() method can be used XHTML documents, but not 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 code The code is as follows:

var p_text = $("p").text(); //获取p元素的文本内容
Copy after login

with the html() method Similarly, if you need to set text content for an element, you also need to pass a parameter. For example, to set text content for the p element, the code is as follows:

Copy code The code is as follows:

// 设置p元素的文本内容 
$("p").text("欢迎您访问简明现代魔法图书馆~~");
Copy after login

Please pay attention to the following two points:
In JavaScript The innerText property does not work 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:

, like this js The file can dynamically obtain the id of the

element, and successfully assign the value through the text() method $("#p" userId).text(name);

The above is the detailed content of js obtains and sets the display value of the p tag through the html() and text() methods. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!