Get the App
SLTechnology News&Howtos  ›  Development  › 

How to replace the content in the tag by jquery

Shulou Source: shulou.com Published: 2022-06-02 02:32:15 10月04日 Update

Jquery how to replace the content in the tag, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

In jquery, you can replace the content in the tag with the html () method, which returns or sets the contents of the selected element. When no parameters are set using this method, the current content of the selected element is returned with the syntax of "$(element). Html (" new content ").

The operating environment of this tutorial: windows10 system, jquery3.2.1 version, Dell G3 computer.

How does jquery replace the content in the tag

The html () method returns or sets the content (inner HTML) of the selected element.

If the method does not set a parameter, it returns the current contents of the selected element.

Returns the content of the element

When you use this method to return a value, it returns the contents of the first matching element.

Grammar

(selector) .html ()

Set element content

When you use this method to set a value, it overwrites the contents of all matching elements.

Grammar

$(selector) .html (content)

Content is optional. Specifies the new content of the selected element. This parameter can contain a HTML tag.

Use a function to set the content of an element

Use the function to set the contents of all matching elements.

Grammar

$(selector) .html (function (index,oldcontent))

Function (index,oldcontent) specifies a function that returns the new content of the selected element.

Index-optional. The index location of the receive selector.

Oldcontent-optional. Receives the current contents of the selector.

Examples are as follows:

$(document) .ready (function () {$(".btn1") .click (function () {$("p"). Html ("this is new");})

This is the old content.

This is also the old content.

Change the content of p element

Output result:

After clicking the button:

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Tags: Content elements methods tags syntax functions parameters new content this is help selection clarity location function for this that is buttons tutorials articles beginners Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology MySQL OPPO Reno macOS NVidia