Home >Web Front-end >JS Tutorial >Usage examples of html() method in jQuery_jquery

Usage examples of html() method in jQuery_jquery

WBOY
WBOYOriginal
2016-05-16 16:24:341196browse

The example in this article describes the usage of the html() method in jQuery. Share it with everyone for your reference. The specific analysis is as follows:

This method can set and get the HTML content of the matching element, and the original content will be replaced by the newly set content.

Special instructions:

HTML content means that the content can contain HTML tags and can be rendered by the browser.
The text content is to first convert the HTML predefined characters in the content into HTML character entities, so that the HTML tags will not be rendered.
Grammar structure 1:

Copy code The code is as follows:
$(selector).html()

At this time, when the method takes no parameters, it obtains the html content of the first matching element.

This method is similar to the text() method without parameters, but there are still big differences:

1. The html() method obtains the content of the first matching element, while the text() method obtains the content contained in all matching elements.
2. The html() method obtains the html content, while the text() method obtains the text content.

Example code:

Copy code The code is as follows:






Script Home







  • Script House welcomes you






The above code will return the content in the div element.
Grammar structure 2:

Copy code The code is as follows:
$(selector).html(content)

With parameters, it sets the html content of all matching elements.
This method is similar to the text() method with parameters, but there are still big differences:
The html() method sets the html content, while the text() method sets the text content.

Example code:

The following code sets the html content in the div to "I am the reset content".

Copy code The code is as follows:






Script Home





Original content




实例二:

复制代码 代码如下:






脚本之家










通过此实例大家可以观察一下html内容和文本内容的区别。

希望本文所述对大家的jQuery程序设计有所帮助。

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