In jquery, you can use the attr() method to add an id to an element; this method can add specified attributes to the element and set the specified attribute value. You only need to set the value of its first parameter to " id", the second parameter can be set to the specified id value, the syntax is "specified element object.attr("id","specified id 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 an id to an element.
attr() method can add specified attributes to elements and set specified attribute values.
This method accepts two parameters:
$(selector).attr(attribute,value)
Parameters | Description |
---|---|
##attribute | Specifies the name of the attribute.|
value | Specifies the value of the attribute.
attributes of the method to
id, the second parameter
value can be set to the specified id value.
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="js/jquery-1.10.2.min.js"></script> <style> #div{ background-color: #FF0000; padding: 10px; margin: 10px; } </style> <script> $(document).ready(function() { $("button").on("click", function() { $("div").attr("id","div"); }); }); </script> </head> <body class="ancestors"> <div>这是一段测试文本</div> <button>给div元素增加id</button> </body> </html>
jQuery video tutorial, web front-end video]
The above is the detailed content of How to add id to element in jquery. For more information, please follow other related articles on the PHP Chinese website!