The attr of method in jQuery USES a summary

  • 2020-06-03 05:52:30
  • OfStack

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 mode is also different.

Example 1

Sets 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> Set the image's  width  attribute </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> Returns the width of the image </button>
</body>
</html>

This is the end of this article, I hope you enjoy it.


Related articles: