jquery 属性 attr

:attr

attr(name)

返回匹配集合中第一个元素的属性‘name’的属性值。如果该元素没有该属性则返回‘未定义’,属性包括title, alt, src, href, width, style, 等等

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <script src="http://code.jquery.com/jquery-latest.js"></script> <script> $(document).ready(function(){ var title = $("em").attr("title"); $("div").text(title); }); </script> <style> em { color:blue; font-weight;bold; } div { color:red; } </style> </head> <body> <p> Once there was a <em title="huge, gigantic">large</em> dinosaur... </p> The title of the emphasis is:<div></div> </body> </html>

 

 

var title = $("em").attr("title");
返回匹配em元素集合中第一个em元素里title属性值。以下是属性值

<em title="huge, gigantic">

 

 

 

attr(properties)

将元素属性以及属性值以键值对的形式注入匹配集合每个元素中。并将覆盖元素原有属性值。

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <script src="http://code.jquery.com/jquery-latest.js"></script> <script> $(document).ready(function(){ $("img").attr({ src: "/images/hat.gif", title: "jQuery", alt: "jQuery Logo" }); $("div").text($("img").attr("alt")); }); </script> <style> img { padding:10px; } div { color:red; font-size:24px; } </style> </head> <body> <img /> <img /> <img /> <div></div> </body> </html>

 

  

$("img").attr({ 。。。});

往img元素集合里注入属性以及对于属性值。以下为注入属性及属性值
src: "/images/hat.gif",
title: "jQuery",
alt: "jQuery Logo"

 

 

 

attr(key,value)

将一个单独属性及对于属性值注入到匹配元素集合中每个元素。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <script src="http://code.jquery.com/jquery-latest.js"></script> <script> $(document).ready(function(){ $("button:gt(1)").attr("disabled","disabled"); }); </script> <style> button { margin:10px; } </style> </head> <body> <button>0th Button</button> <button>1st Button</button> <button>2nd Button</button> </body> </html>

 

 

$("button:gt(1)").attr("disabled","disabled");

将disabled属性以及对应属性值‘disabled’注入到匹配button元素集合的每个button元素里。以下是注入信息

"disabled","disabled"

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值