Home  >  Article  >  Web Front-end  >  Can jquery get custom attributes?

Can jquery get custom attributes?

WBOY
WBOYOriginal
2022-05-18 16:58:032084browse

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)".

Can jquery get custom attributes?

The operating environment of this tutorial: windows10 system, jquery3.2.1 version, Dell G3 computer.

Can jquery get custom attributes?

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:

Can jquery get custom attributes?

After clicking the button:

Can jquery get custom attributes?

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!

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