Home  >  Article  >  Web Front-end  >  Summary of using attr() method in jQuery_jquery

Summary of using attr() method in jQuery_jquery

WBOY
WBOYOriginal
2016-05-16 16:01:391248browse

Definition and usage

The

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.

Example 1

Set the attributes and values ​​of the selected element.

<html>
<head>
<script type="text/javascript" src="/jquery/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
$("img").attr("width","180");
});
});
</script>
</head>
<body>
<img src="/i/eg_smile.gif" />
<br />
<button>设置图像的 width 属性</button>
</body>
</html>

Example 2

Returns the attribute value of the selected element.

<html>
<head>
<script type="text/javascript" src="/jquery/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
alert("Image width " + $("img").attr("width"));
});
});
</script>
</head>
<body>
<img src="/i/eg_smile.gif" width="128" height="128" />
<br />
<button>返回图像的宽度</button>
</body>
</html>

The above is the entire content of this article, I hope you all like it.

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