`
KAXU
  • 浏览: 267279 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

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"

 

 

 

 

 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics