在JavaScript中,attr是一种属性操作方法,该方法可以设置或返回被选元素的属性值,语法为“$(selector).attr(attribute)”或者“$(selector).attr(attribute,value)”。

本教程操作环境:windows10系统、javascript1.8.5版、Dell G3电脑。
attr() 方法设置或返回被选元素的属性值。
返回被选元素的属性值。
语法
立即学习“Java免费学习笔记(深入)”;
$(selector).attr(attribute)
attribute 规定要获取其值的属性。
设置被选元素的属性和值。
语法
立即学习“Java免费学习笔记(深入)”;
$(selector).attr(attribute,value)
attribute 规定属性的名称。
value 规定属性的值。
设置被选元素的属性和值。
$(selector).attr(attribute,function(index,oldvalue))
attribute 规定属性的名称。
function(index,oldvalue)规定返回属性值的函数。
该函数可接收并使用选择器的 index 值和当前属性值。
为被选元素设置一个以上的属性和值。
语法
立即学习“Java免费学习笔记(深入)”;
$(selector).attr({attribute:value, attribute:value ...})attribute:value 规定一个或多个属性/值对。
示例如下:
<html>
<head>
<script type="text/javascript" src="/jquery/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
$("img").attr("width",function(n,v){
return v-50;
});
});
});
</script>
</head>
<body>
<img src="/i/eg_smile.gif" width="128" height="128" />
<br />
<button>减少图像的宽度 50 像素</button>
</body>
</html>输出结果:

点击按钮后:

相关推荐:javascript学习教程
以上就是javascript中attr是什么意思的详细内容,更多请关注php中文网其它相关文章!
java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号