jQuery 属性操作 - attr() 方法
2015-04-23 23:32
866 查看
定义和用法
attr() 方法设置或返回被选元素的属性值。根据该方法不同的参数,其工作方式也有所差异。
[b]实例1[/b]
设置被选元素的属性和值。
<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>
[b]实例2[/b]
返回被选元素的属性值。
<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>
相关文章推荐
- jquery之操作元素属性和特性(设置特性值,attr(name,value),attr(attributes)方法的使用)
- jquery之操作元素属性和特性(获取特性值,attr(name)方法的使用)
- jQuery .attr()和.removeAttr()方法操作元素属性示例
- jQuery 属性操作 - attr() 方法
- jquery之操作元素属性和特性(删除特性,removeAttr()方法的使用)
- jQuery 属性操作 - attr() 方法
- jQuery 属性操作 - removeAttr() 方法
- jQuery 属性操作 - removeAttr() 方法
- Jquery操作元素属性方法总结(attr()、prop())
- jQuery 属性操作 - attr() 方法
- jQuery .attr()和.removeAttr()方法操作元素属性示例
- jQuery 属性操作 - attr() 方法
- jQuery .attr()和.removeAttr()方法操作元素属性示例
- jquery属性操作 - attr() 方法
- jQuery 属性操作 - attr() 方法
- jQuery编程基础精华(属性、表单过滤器,元素的each,表单选择器,子元素过滤器(*),追加方法,节点,样式操作)
- 在使用Jquery的时候明明checked属性存在但是attr('checked')却显示undefine的原因以及解决方法
- jQuery核心:DOM操作及属性方法
- jquery 实践操作:attr()方法