首页 > Web开发 > 详细

jQuery 属性操作 - attr() 方法

时间:2015-04-24 00:51:33      阅读:289      评论:0      收藏:0      [点我收藏+]

定义和用法

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 属性操作 - attr() 方法

原文:http://www.cnblogs.com/itdaocaoren/p/4452168.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!