Home  >  Article  >  Web Front-end  >  What does attr mean in jquery

What does attr mean in jquery

WBOY
WBOYOriginal
2022-06-06 19:10:563495browse

In jquery, attr is a method used to set or return the attributes and values ​​of the selected element; when this method is used to return the attribute value, it can return the value of the first matching element; this method is used When setting attribute values, you can set multiple attribute values. The syntax is "element object.attr(attribute)" or "element object.attr(attribute, attribute value)".

What does attr mean in jquery

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

What does attr mean in jquery

attr() method sets or returns the attributes and values ​​of the selected element.

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.

Syntax

Return the value of the attribute:

$(selector).attr(attribute)

Set the attribute and value:

$(selector).attr(attribute,value)

Use the function to set the attribute and value:

$(selector).attr(attribute,function(index,currentvalue))

Set multiple attributes and values:

$(selector).attr({attribute:value, attribute:value,...})

Examples of returned attribute values ​​are as follows:

<!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("图片宽度: " + $("img").attr("width"));
});
});
</script>
</head>
<body>
<img src="img_pulpitrock.jpg" alt="Pulpit Rock" width="284"    style="max-width:90%">
<br>
<button>返回图片的宽度</button>
</body>
</html>

Output results;

What does attr mean in jquery

##Click After the button:

What does attr mean in jquery

Recommended video tutorial:

jQuery video tutorial

The above is the detailed content of What does attr mean in jquery. 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