Home > Article > Web Front-end > How to add attributes to tags in jquery
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"})".
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.
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.
In the test.html file, use the p tag to create a line of test text.
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.
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. .
Open the test.html file in the browser, click the button to view the source code and view the results.
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!