Home > Article > Web Front-end > Can jquery get custom attributes?
jquery can obtain custom attributes; you can use the attr() method to obtain custom attributes. This method is used to set or return the attribute value of the selected element, including custom attributes. When the parameter only specifies When obtaining the attribute, the specified attribute value will be returned, and the syntax is "element object.attr (custom attribute)".
The operating environment of this tutorial: windows10 system, jquery3.2.1 version, Dell G3 computer.
attr() method sets or returns the attribute value of the selected element.
According to the different parameters of this method, its working method is also different.
Return the attribute value of the selected element.
Syntax
$(selector).attr(attribute)
attribute specifies the attribute whose value is to be obtained.
Examples are as follows:
Get attributes-attr()
<div id="tiny" value="1090"></div>
jquery value:
$("#tiny").attr("value");
Get custom attributes
<div id="tiny" value="1090" data_obj="userDefineAttr"></div>
jquery value:
$("#tiny").attr("data_obj");
The example is as follows:
<html> <head> <script type="text/javascript" src="/jquery/jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("button").click(function(){ alert($("img").attr("city")); }); }); </script> </head> <body> <img src="/i/eg_smile.gif" style="max-width:90%" style="max-width:90%" city="hefei"/ alt="Can jquery get custom attributes?" > <br /> <button>返回图像的宽度</button> </body> </html>
Output result:
After clicking the button:
Recommended related video tutorials: jQuery video tutorial
The above is the detailed content of Can jquery get custom attributes?. For more information, please follow other related articles on the PHP Chinese website!