jquery can get the content of the id in the element; it can be obtained by using the attr() method. This method is used to set or return the attributes and values of the selected element. When used to return the attribute value of the specified element, return is just the value of the first matching element, the syntax is "$(selector).attr("id")".
The operating environment of this tutorial: windows10 system, jquery3.2.1 version, Dell G3 computer.
jquery can get the content of the id in the element
attr() method sets or returns the attributes of the selected element and value.
When this method is used to return an attribute value, the value of the first matching element is returned.
When this method is used to set attribute values, one or more attribute/value pairs are set for the matching element.
The syntax is:
Return the value of the attribute
$(selector).attr(attribute)
Syntax expansion
Set the attribute and value :
$(selector).attr(attribute,value)
Use functions to set properties and values:
$(selector).attr(attribute,function(index,currentvalue))
Set multiple properties and values:
$(selector).attr({attribute:value, attribute:value,...})
The example is as follows:
Get elements The value of the id attribute.
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>123</title> <script src="js/jquery.min.js"> </script> <script> $(document).ready(function(){ $("button").click(function(){ alert("图片的id属性值: " + $("img").attr("id")); }); }); </script> </head> <body> <img src="img_pulpitrock.jpg" alt="Pulpit Rock" width="284" style="max-width:90%" id="picture"> <br> <button>返回图片的id属性值</button> </body> </html>
Output result:
After clicking the button:
Video tutorial recommendation: jQuery video tutorial
The above is the detailed content of Can jquery get the content of the id in the element?. For more information, please follow other related articles on the PHP Chinese website!