Home>Article>Web Front-end> How to change the content of tag in jquery

How to change the content of tag in jquery

青灯夜游
青灯夜游 Original
2022-11-02 19:23:27 1842browse

Two modification methods: 1. Use the jquery selector to obtain the h tag object, and use text() to modify the text content of the object. The syntax is "$("selector").text("new content") ;"; 2. Use the jquery selector to obtain the h tag object, and use html() to modify the content of the object (the content of the text and HTML tag), with the syntax "$("selector").html("new content"); ".

How to change the content of <h> tag in jquery

The operating environment of this tutorial: windows7 system, jquery3.6.1 version, Dell G3 computer.

Two ways to change the content of the tag with jquery

Method 1: Use text() to change the text content

text() can set the text content of the element, which can be changed by simply setting the text content to a new value.

Modification steps:

  • Use jquery selector to obtain the h tag (h1~h6) object

$("选择器")
  • Use text() to modify the content of the obtained object

对象.text("新内容")

Example:

      
   

这是一个大标题。

这是另一个标题。

这是另一个标题。

这是另一个段落。

How to change the content of <h> tag in jquery

Method 2: Use html() changes tag content

html() can set or return content that contains text and HTML tags.

Modification steps:

  • Use jquery selector to obtain the h tag (h1~h6) object

$("选择器")
  • Use html() to modify the content of the obtained object

对象.html("新内容")

Example:

      
   

这是一个大标题。

这是另一个标题。

这是另一个标题。

这是另一个段落。

How to change the content of <h> tag in jquery

Expand knowledge: html () Comparison with text()

html() obtains all the content inside the element, while text() obtains only the text content.

      
  

PHP中文网

html()是:
text()是:

How to change the content of <h> tag in jquery

The difference between the two methods html() and text() can be clearly compared from the following table.

PHP Chinese website
## [Recommended learning:
HTML code html() text()
PHP Chinese website PHP Chinese website
##PHP中文网 PHP中文网
(empty string)
jQuery video tutorial

,web front-end development

The above is the detailed content of How to change the content of tag in jquery. For more information, please follow other related articles on the PHP Chinese website!

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