- 0133技术站
- 联系QQ:18840023
- QQ交流群

- 微信公众号

jQuery attr() 方法
定义和用法
attr() 方法设置或返回被选元素的属性和值。
当该方法用于返回属性值,则返回第一个匹配元素的值。
当该方法用于设置属性值,则为匹配元素设置一个或多个属性/值对。
语法
返回属性的值:
$(selector).attr(attribute)
设置属性和值:
$(selector).attr(attribute,value)
使用函数设置属性和值:
$(selector).attr(attribute,function(index,currentvalue))
设置多个属性和值:
$(selector).attr({attribute:value, attribute:value,...})
参数 | 描述 |
attribute | 规定属性的名称 |
value | 规定属性的值。 |
function(index,currentvalue) | 规定要返回属性值到集合的函数 index : 接受集合中元素的 index 位置。 currentvalue : 接受被选元素的当前属性值。 |
<script> $(document).ready(function(){ $("button").click(function(){ $("img").attr("width","300px"); }); }); </script> </head> <body> <img src="./images/44.jpg" width="100px" height="100px"> <br> <button>为图片设置width属性</button>
点击 "运行实例" 按钮查看在线实例
效果图:
推荐手册