jQuery中 attr() 方法使用小结
更新时间:2015年05月03日 10:22:56 投稿:hebedich
jquery中用attr()方法来获取和设置元素属性,attr是attribute(属性)的缩写,在jQuery DOM操作中会经常用到attr()
定义和用法
attr() 方法设置或返回被选元素的属性值。
根据该方法不同的参数,其工作方式也有所差异。
实例1
设置被选元素的属性和值。
<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>
实例2
返回被选元素的属性值。
<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实现可定制的web在线富文本编辑器附源码下载
UEditor的功能非常强大,官方已配备了php,asp,asp.net,java等语言的上传程序,拿来就可以使用,当然ueditor更具有功能插件接口,很轻松地添加自己定义功能到编辑器中,方便项目的不同需求2015-11-11


最新评论