
jquery attr()方法
attr()方法可以设置或返回被选元素的属性值。
根据该方法不同的参数,其工作方式也有所差异。
当该方法用于返回属性值,则返回第一个匹配元素的值。
当该方法用于设置属性值,则为匹配元素设置一个或多个属性/值对。
返回值:返回被选元素的属性值。
语法
返回属性的值:
$(selector).attr(attribute)
设置属性和值:
$(selector).attr(attribute,value)
使用函数设置属性和值:
$(selector).attr(attribute,function(index,currentvalue))
设置多个属性和值:
$(selector).attr({attribute:value, attribute:value,...})参数:

示例:为元素返回属性的值
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
alert("图片宽度: " + $("img").attr("width"));
});
});
</script>
</head>
<body>
<img src="img_pulpitrock.webp" alt="Pulpit Rock" width="284" height="213">
<br>
<button>返回图片的宽度</button>
</body>
</html>效果图:

更多web前端知识,请查阅 HTML中文网 !!
以上就是jquery怎么获取属性值?的详细内容,更多请关注易知道|edz.cc其它相关文章!














