Home >Web Front-end >JS Tutorial >js uses the html() or text() method to obtain the displayed value of the p tag_javascript skills

js uses the html() or text() method to obtain the displayed value of the p tag_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:40:361820browse

html() method

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

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

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

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

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

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

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

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

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 e388a4556c0f65e1904146cc1a846bee94b3e26ee717c64999d7867364b1b4a3 element that displays the friend’s name: 0902c5e87743c2c26b0dd04c4b8b3f6694b3e26ee717c64999d7867364b1b4a3, like this js The file can dynamically obtain the id of the e388a4556c0f65e1904146cc1a846bee94b3e26ee717c64999d7867364b1b4a3 element, and successfully assign the value through the text() method $("#p" userId).text(name);

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