Home > Web Front-end > Front-end Q&A > Can jquery get custom attributes?

Can jquery get custom attributes?

WBOY
Release: 2022-05-18 16:58:03
Original
2125 people have browsed it

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)
Copy after login

attribute specifies the attribute whose value is to be obtained.

Examples are as follows:

Get attributes-attr()

<div id="tiny" value="1090"></div>
Copy after login

jquery value:

 $("#tiny").attr("value");
Copy after login

Get custom attributes

<div id="tiny" value="1090" data_obj="userDefineAttr"></div>
Copy after login

jquery value:

$("#tiny").attr("data_obj");
Copy after login

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>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template