JQuery学习二:.attr()

来源:互联网 发布:中国网络墙之父 编辑:程序博客网 时间:2024/05/16 17:17

JQuery属性操作-attr()

定义和方法:返回或设置被选元素的属性值。

语法一:

$(selector).attr(attribute,value)

举例一:
<html><head><script type="text/javascript" src="/jquery/jquery.js"></script><script type="text/javascript">$(document).ready(function(){  $("button").click(function(){  <span style="color:#cc0000;">  $("img").attr("width","180");</span>  });});</script></head><body><span style="color:#33cc00;"><img src="/i/eg_smile.gif" /></span><br /><button>设置图像的 width 属性</button></body></html>

语法二:
$(selector).attr(attribute,function(index,oldvalue))
举例二:
<html><head><script type="text/javascript" src="/jquery/jquery.js"></script><script type="text/javascript">$(document).ready(function(){  $("button").click(function(){    $("img").attr("width",<span style="color:#cc0000;"><strong>function(n,v){      return v-50;    });</strong></span>  });});</script></head><body><span style="color:#009900;"><img src="/i/eg_smile.gif" width="128" height="128" /><br /></span><button>减少图像的宽度 50 像素</button></body></html>
 
语法三:
$(selector).attr({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(){    <span style="color:#cc0000;">$("img").attr({width:"50",height:"80"});</span>  });});</script></head><body><span style="color:#33cc00;"><img src="/i/eg_smile.gif" /></span><br /><button>设置图像的 width 和 height 属性</button></body></html>



0 0