Home  >  Article  >  Web Front-end  >  How to add attributes to tags in jquery

How to add attributes to tags in jquery

青灯夜游
青灯夜游Original
2021-11-22 09:49:466493browse

In jquery, you can use the attr() method to add attributes to the label. This method can set the attribute value of the selected element. The syntax "$(selector).attr("Attribute name","Attribute value ")" or "$(selector).attr({attribute name:"attribute value"})".

How to add attributes to tags in jquery

The operating environment of this tutorial: windows7 system, jquery1.10.2 version, Dell G3 computer.

In jquery, you can use the attr() method to add attributes to tags; this method can set or return the attribute value of the selected element. According to the different parameters of this method, its working method is also different.

Grammar:

$(selector).attr("属性名","属性值")
$(selector).attr({属性名:"属性值"})

Let’s take a look at how to add attributes to tags using the attr() method:

Create a new html file , named test.html, is used to explain how jquery adds attributes to tags.

How to add attributes to tags in jquery

In the test.html file, introduce the jquery.min.js library file and successfully load the file before you can use the methods in jquery.

How to add attributes to tags in jquery

In the test.html file, use the p tag to create a line of test text.

How to add attributes to tags in jquery

In the test.html file, use the button tag to create a button, bind the onclick click event to the button button, and when the button is clicked, execute the addsx() function.

How to add attributes to tags in jquery

In the js tag, create the addsx() function. Within the function, obtain the p tag object through the element name, and use the attr() method to set the id of the p tag to mypp. .

How to add attributes to tags in jquery

Open the test.html file in the browser, click the button to view the source code and view the results.

How to add attributes to tags in jquery

How to add attributes to tags in jquery

Recommended related video tutorials: jQuery Tutorial (Video)

The above is the detailed content of How to add attributes to tags 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