jQuery Attributes(属性)的使用(一、属性篇)

  作者:bea

本篇讲解:attr(name),attr(properties),attr(key,value),attr(key,fn),removeAttr(name)的用法。 您对本系列文章有任何建议或意见请发送到邮箱:sjzlgt@qq.com 您可以到jQuery官网来学习更多的有关jQuery知识。 下面的代码,运行后,需要刷新下。 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://
本篇讲解:attr(name),attr(properties),attr(key,value),attr(key,fn),removeAttr(name)的用法。
您对本系列文章有任何建议或意见请发送到邮箱:sjzlgt@qq.com
您可以到jQuery官网来学习更多的有关jQuery知识。
下面的代码,运行后,需要刷新下。



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>jQuery-Attributes-1</title>
<script src="http://img./jslib/jquery/jquery-1.3.2.min.js" type="text/javascript"></script>
<style type="text/css">
.div
{
width:95%;
margin-left:15px;
margin-top:15px;
margin-right:15px;
padding-left:5px;
padding-top:5px;
padding-bottom:5px;
background-color:#ccc;
border:#999 1px solid;
line-height:30px;
font-size:13px;
font-family:微软雅黑;
}
.blue{color:Blue;}
.green{color:Green;}
.button{border:green 1px solid;width:100px;}
.xiaobiao{ font-weight:bold;}
.red_test{background-color:red; color:White; width:300px; height:30px;}
.li_test{border:#000 1px solid;}
</style>
</head>
<body>



<div class="div">
<div style="width:100%; text-align:center; font-size:16px; font-weight:bold;">jQuery-Attributes(属性)的使用(一、属性篇)</div>
本系列文章主要讲述jQuery框架的属性(Attributes)使用方法,我将以实例方式进行讲述,以简单,全面为基础,不会涉及很深,文章分为:属性篇、类篇、Html代码篇&文本篇、值篇共4篇文章。<br>
本篇讲解:attr(name),attr(properties),attr(key,value),attr(key,fn),removeAttr(name)的用法。<br>
您对本系列文章有任何建议或意见请发送到邮箱:sjzlgt@qq.com <br>
您可以到<a href="http://jquery.com/" target="_blank">jQuery官网</a>来学习更多的有关jQuery知识。<br>
<span style="color:Red;"><strong>版权所有:code-cat 博客:<a href="http://www.cnblogs.com/bynet/" target="_blank">http://www.cnblogs.com/bynet</a> 转载请保留原作者、出处和版权信息!</strong></span>
</div>

<div class="div">在IE6/7/8、火狐下测试过了,效果都行实现。</div>

<div class="div">
<span class="xiaobiao">1. attr(name)用法</span><br>
定义:取得第一个匹配元素的属性值。通过这个方法可以方便地从第一个匹配元素中获取一个属性的值。如果元素没有相应属性,则返回 undefined 。<br>
返回值:Object<br>
参数:name (String) : 属性名称<br>
实例:将ID为"div_a1"的DIV中第一张图片的路径、ID、Name、宽度、Alt、Value属性的值输出到ID为'div_a1_1'的DIV中。<br>
<div style="border:blue 1px solid;">
代码: <br>
$("#div_a1_1").append('<span style="color:Red;"><strong>图片数量:$("#div_a1 img")获取到'+$("#div_a1 img").size()+'张图片</strong></span><br />');<br>
$("#div_a1_1").append('第一张图片路径:'+$("#div_a1 img").attr("src")+'<br />');<br>
$("#div_a1_1").append('第一张图片ID:'+$("#div_a1 img").attr("id")+'<br />');<br>
$("#div_a1_1").append('第一张图片Name:'+$("#div_a1 img").attr("name")+'<br />');<br>
$("#div_a1_1").append('第一张图片框宽度:'+$("#div_a1 img").attr("width")+'<br />');<br>
$("#div_a1_1").append('第一张图片框alt:'+$("#div_a1 img").attr("alt")+'<br />');<br>
$("#div_a1_1").append('<span style="color:Red;"><strong>第一个图片框Value值:'+$("#div_a1 img").attr("Value")+'</strong></span><br />');
<span style="color:green;"> //点击按钮一看效果</span>
</div><br>
<div id="div_a1" style="border:red 1px solid; padding-bottom:5px; padding-left:5px; width:98%;">
DIV ID="div_a1"<br>
<img id="img_1" name="img_bky" src="http://files./upload/2009-12/20091214233425668.gif" width="129" height="38" alt="Google" border="0" />
<img id="img_2" name="img_yh" src="http://files./upload/2009-12/20091214233425172.gif" width="140" height="23" alt="雅虎" border="0" />
<input type="text" id="txt_1" name="txt_bky" style="width:150px;" value="input text 博客园" />
<br>
<div id="div_a1_1" style="width:400px; border:#000 1px solid;">
id="div_a1_1"<br>
</div>
<input type="button" id="btn_1" value="按钮一" class="button" /><br>
你可以试着将上面那个文本框的属性值输出
<script type="text/javascript">
$("#btn_1").click(function(){
$("#div_a1_1").append('<span style="color:Red;"><strong>图片数量:$("#div_a1 img")获取到'+$("#div_a1 img").size()+'张图片</strong></span><br>');
$("#div_a1_1").append('第一张图片路径:'+$("#div_a1 img").attr("src")+'<br>');
$("#div_a1_1").append('第一张图片ID:'+$("#div_a1 img").attr("id")+'<br>');
$("#div_a1_1").append('第一张图片Name:'+$("#div_a1 img").attr("name")+'<br>');
$("#div_a1_1").append('第一张图片框宽度:'+$("#div_a1 img").attr("width")+'<br>');
$("#div_a1_1").append('第一张图片框alt:'+$("#div_a1 img").attr("alt")+'<br>');
$("#div_a1_1").append('<span style="color:Red;"><strong>第一个图片框Value值:'+$("#div_a1 img").attr("Value")+'</strong></span><br>');
});
</script>
</div>
<span style="color:Red;"><strong>注意:选择器$("#div_a1 img")获取到了2张图片,而att(name)只得到第一张图片的属性值。img元素没有value属性,所以最后输出'undefined'。</strong></span>
</div>



<div class="div">
<span class="xiaobiao">2. attr(properties)用法</span><br>
定义:将一个"名/值"形式的对象设置为所有匹配元素的属性。<br>
这是一种在所有匹配元素中批量设置很多属性的最佳方式。 注意,如果你要设置对象的class属性,你必须使用'className' 作为属性名。
或者你可以直接使用.addClass( class ) 和 .removeClass( class ).
<br>
返回值:jQuery<br>
参数:properties (Map) : 作为属性的"名/值对"对象<br>
实例:将ID为"div_b1"的DIV中为所有图像设置src和alt属性。<br>
<div style="border:blue 1px solid;">
代码:<br>
$("#div_b1 img:eq(0)").attr({ src: "/upload/2009-12/20091214233425668.gif", alt: "Google" }); <br>
$("#div_b1 img:eq(1)").attr({ src: "/upload/2009-12/20091214233425172.gif", alt: "雅虎" });<span style="color:green;"> //点击按钮二看效果</span>
</div><br>
<div id="div_b1" style="border:red 1px solid; padding-bottom:5px; padding-left:5px; width:98%;">
DIV ID="div_b1"<br>
<img id="img1" name="img_bky" width="129" height="38" border="0" />
<img id="img2" name="img_yh" width="140" height="23" border="0" />
<br>
<input type="button" id="btn_2" value="按钮二" class="button" />
<script type="text/javascript">
$("#btn_2").click(function(){
$("#div_b1 img:eq(0)").attr({ src: "/upload/2009-12/20091214233425668.gif", alt: "Google" });
$("#div_b1 img:eq(1)").attr({ src: "/upload/2009-12/20091214233425172.gif", alt: "雅虎" });
});
</script>
</div>
</div>



<div class="div">
<span class="xiaobiao">3. attr(key,value)用法</span><br>
定义:为所有匹配的元素设置一个属性值。<br>
返回值:jQuery<br>
参数:key (String) : 属性名称 value (Object) : 属性值<br>
实例:将ID为"div_c1"的DIV中为所有图像设置src属性。<br>
<div style="border:blue 1px solid;">
代码:<br>
$("#div_c1 img:eq(0)").attr("src","/upload/2009-12/20091214233425668.gif"); <br>
$("#div_c1 img:eq(1)").attr("src","/upload/2009-12/20091214233425172.gif"); <span style="color:green;"> //点击按钮三看效果</span>
</div><br>
<div id="div_c1" style="border:red 1px solid; padding-bottom:5px; padding-left:5px; width:98%;">
DIV ID="div_c1"<br>
<img id="img3" name="img_bky" width="129" height="38" border="0" />
<img id="img4" name="img_yh" width="140" height="23" border="0" />
<br>
<input type="button" id="btn_3" value="按钮三" class="button" />
<script type="text/javascript">
$("#btn_3").click(function(){
$("#div_c1 img:eq(0)").attr("src","/upload/2009-12/20091214233425668.gif");
$("#div_c1 img:eq(1)").attr("src","/upload/2009-12/20091214233425172.gif");
});
</script>
</div>
</div>


<div class="div">
<span class="xiaobiao">4. attr(key,fn)用法</span><br>
定义:为所有匹配的元素设置一个计算的属性值。不提供值,而是提供一个函数,由这个函数计算的值作为属性值。<br>
返回值:jQuery<br>
参数:key (String) : 属性名称 fn (Function) : 返回值的函数 范围:当前元素, 参数: 当前元素的索引值<br>
实例:将ID为"div_d1"的DIV中为所有图像把title属性的值设置为src属性的值。 <br>
<span style="border:blue 1px solid;">代码:$("#div_d1 img").attr("src", function() { return this.title;}); <span style="color:green;"> //点击按钮四看效果</span></span>
<div id="div_d1" style="border:red 1px solid; padding-bottom:5px; padding-left:5px; width:98%;">
DIV ID="div_d1"<br>
<img id="img5" name="img_bky" width="129" height="38" border="0" title="/upload/2009-12/20091214233425668.gif" />
<img id="img6" name="img_yh" width="140" height="23" border="0" title="/upload/2009-12/20091214233425172.gif" />
<br>
<input type="button" id="btn_4" value="按钮四" class="button" /><br>
<script type="text/javascript">
$("#btn_4").click(function(){
$("#div_d1 img").attr("src", function() { return this.title;});
});
</script>
</div>
</div>



<div class="div">
<span class="xiaobiao">5. removeAttr(name)用法</span><br>
定义:从每一个匹配的元素中删除一个属性。<br>
返回值:jQuery<br>
参数:name (String) : 要删除的属性名<br>
实例:将ID为"div_e1"的DIV中为把所有图像src属性删除 。 <br>
<span style="border:blue 1px solid;">代码:$("#div_e1 img").removeAttr("src"); <span style="color:green;"> //点击按钮五看效果</span></span>
<div id="div_e1" style="border:red 1px solid; padding-bottom:5px; padding-left:5px; width:98%;">
DIV ID="div_e1"<br>
<img id="img7" name="img_bky" width="129" height="38" border="0" src="http://files./upload/2009-12/20091214233425668.gif" />
<img id="img8" name="img_yh" width="140" height="23" border="0" src="http://files./upload/2009-12/20091214233425172.gif" />
<br>
<input type="button" id="btn_5" value="按钮五" class="button" /><br>
<script type="text/javascript">
$("#btn_5").click(function(){
$("#div_e1 img").removeAttr("src");
});
</script>
</div>
</div>
</body>
</html>




[Ctrl+A 全选 注:
如需引入外部Js需刷新才能执行]


完整代码打包下载


有用  |  无用

猜你喜欢