Jquery获取自定义属性(attr和prop)

$("form").attr("check"); 
$("form").prop("check"); 
两种都可以,不过新版jquery推荐第二种,两个在其他方面都差不多,我发现的唯一不同就是在checkbox上的时候,需要用prop,不然IE浏览器会不兼容
//=====================================================================================
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript" src="/js/jq1.3.2.js"></script>
</head>
<body>
<div lang="rrery"> </div>
 <div data-url="rrery"> </div>
       <div data-url="rrrrrrrrrrrrrrttttttttttttttttttttttgggggggggggggggggggggg"> </div>
    <div data-url="rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrtttttttttttttttttttttttttttt777777777777777777777777777777777778888888888455rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrtttttttttttttttttttttttttttt777777777777777777777777777777777778888888888888"> </div>
</body>
</html>
<script>
   // var J = $("div[lang]").get();
   // alert($("[data-url]:eq(2)").attr("data-url"));
    $("[data-url]").each(function () {
        alert($(this).attr("data-url"));
    });
 
//    $("[data-url]").each(function () {
//        alert($(this).prop("data-url"));
//    });
 
</script>

附:  jquery attr()方法

 jquery中用attr()方法来获取和设置元素属性,attr是attribute(属性)的缩写,在jQuery DOM操作中会经常用到attr(),attr()有4个表达式。

1. attr(属性名       //获取属性的值(取得第一个匹配元素的属性值。通过这个方法可以方便地从第一个匹配元素中获取一个属性的值。如果元素没有相应属性,则返回 undefined )

2. attr(属性名, 属性值)   //设置属性的值 (为所有匹配的元素设置一个属性值。

3. attr(属性名,函数值    //设置属性的函数值  (为所有匹配的元素设置一个计算的属性值。不提供值,而是提供一个函数,由这个函数计算的值作为属性值。

4.attr(properties)    //给指定元素设置多个属性值,即:{属性名一: “属性值一” , 属性名二: “属性值二” , … … }。(这是一种在所有匹配元素中批量设置很多属性的最佳方式。 注意,如果你要设置对象的class属性,你必须使用'className' 作为属性名。或者你可以直接使用'class'或者'id'。)

 

示例代码:

<!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> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>jquery中attr()方法</title> <script src="js/jquery-1.4.2.min.js" language="javascript" type="text/javascript" ></script> <style> p{color:red} li{color:blue;} .lili{font-weight:bold;color:red;}

#lili{font-weight:bold;color:red;} </style> </head> <body> <p title="你最喜欢的水果是。">你最喜欢的水果是?</p> <ul> <li title="苹果汁">苹果</li> <li title="橘子汁" alt="123">橘子</li> <li title="菠萝汁">菠萝</li> </ul> <script> ... </script> </body> <html>

1.attr(name)//获取属性的值

1.1使用attr(name)获取title值:

<script> alert($("ul li:eq(1)").attr("title")); </script>

1.2使用attr(name)获取alt值:

<script> alert($("ul li:eq(1)").attr("alt")); </script>

2. attr(name,value)   //设置属性的值

2.1使用attr(name,value)修改title值为:不吃橘子 <script> $("ul li:eq(1)").attr("title","不吃橘子"); alert($("ul li:eq(1)").attr("title")); </script>

3. attr(name,fn)  //设置属性的函数值

3.1把alt属性的值设置为title属性的值。 <script> $("ul li:eq(1)").attr("title",function(){ return this.alt}); alert($("ul li:eq(1)").attr("title")); </script>

4.attr(properties)  //将一个“名/值”形式的对象设置为所有匹配元素的属性

4.1获取<ul>里第2个<li>设置title和alt属性。 <script> $("ul li:eq(1)").attr({title:"不喝橘子汁",alt:"不是123"}); alert($("ul li:eq(1)").attr("title")); alert($("ul li:eq(1)").attr("alt")); </script>

4.2获取<ul>里第2个<li>设置class。
<script>
$("ul li:eq(1)").attr({className:"lili"});
</script>

4.3获取<ul>里第2个<li>设置id。
<script>
$("ul li:eq(1)").attr({id:"lili"});
</script>

4.4获取<ul>里第2个<li>设置style。
<script>
$("ul li:eq(1)").attr({style:"color:red"});
</script>

在li中添加alt是错误的,它只能用在img、area和input元素中(包括applet元素)。对于input元素,alt属性意在用来替换提交按钮的图片。在这里为了很详细说明attr()方法,没有合适的属性,所有用了alt进行举例,只供学习参考attr()方法用法。 在此说明下alt和tite的区别。 alt:这是用以描述图形的文字,当图片无法显示时,这些文字会替代图片而被显示。当鼠标移至图片上该些文字亦会显示。 title:是鼠标放上去之后,会显示出来的文字。

那么怎么删除属性呢?

 

jquery中删除属性的关键词是: removeAttr 注意A是大写的看看怎么用的:

 

同样是用法一中的html代码我想删掉li的title属性那么就这样:

 

<script> $("ul li:eq(1)").removeAttr ("title"); </script>

就这么简单, attr 其实就是原生js中 getAttribute 的简化实现removeAttr 就是 removeAttribute 的简写了。

那么是否有跟attr()相似的属性呢? jquery中val()与之类似, $(this).val();获取某个元素节点的value值,相当于$(this).attr("value"); $(this).val(value);设置某个元素节点的value值,相当于$(this).attr("value",value);

 

jquery1.6中的.prop()和.attr()异同

最近在iteye的新闻中看到jQuery已经更新到了1.6.1。和之前版本的最大变化是增加了.prop方法。但是.prop()方法和.attr()方法,单从字面上很难区分。在汉语中properties和attributes都有表示“属性”的意思。 下面根据这篇博文(javascript:mctmp(0);),简要翻译了.prop()和.attr()的用法:

1、从1.5.2升级到1.6.1

通过介绍新方法.prop()以及.attr()方法的改变,jQuery1.6.1引起了一场关于attributes和properties之间有何区别和联系的激烈讨论。同时,1.6.1也解决了一些向后兼容性问题。当从1.5.2升级到1.6.1时,你不必修改任何attribute代码。

下面是关于jQuery1.6和1.6.1中Attributes模块变化的描述,以及.attr()方法和.prop()方法的首选使用。然而,正如前面所述,jQuery1.6.1允许你使用.attr()方法就像以前它被使用在所有的情况中一样。

 2、发生了什么变化

Attributes模块的变化是移除了attributes和properties之间模棱两可的东西,但是在jQuery社区中引起了一些混乱,因为在1.6之前的所有版本中都使用一个方法(.attr())来处理attributes和properties。但是老的.attr()方法有一些bug,很难维护。jQuery1.6.1对Attributes模块进行了更新,并且修复了几个bug。

特别提到的是,boolean attributes,比如:checked,selected,readonly和disabled在1.6.1中和1.6之前的处理相同。这意味着下面的代码: 

Js代码 复制代码 收藏代码
  1. $(“:checkbox”).attr(“checked”, true);  
  2. $(“option”).attr(“selected”, true);  
  3. $(“input”).attr(“readonly”, true);  
  4. $(“input”).attr(“disabled”, true);  
$(“:checkbox”).attr(“checked”, true);
$(“option”).attr(“selected”, true);
$(“input”).attr(“readonly”, true);
$(“input”).attr(“disabled”, true);

 甚至是这样的代码:

Js代码 复制代码 收藏代码
  1. if ( $(“:checkbox”).attr(“checked”) ) { /* Do something */ }  
if ( $(“:checkbox”).attr(“checked”) ) { /* Do something */ }

 在1.6.1中没有必要为了保持之前期望的运行结果而发生任何改变。

 为了让jQuery1.6中的.attr()方法的变化被理解的清楚些,下面是一些使用.attr()的例子,虽然在jQuery之前的版本中能正常工作,但是现在必须使用.prop()方法代替:

 首先,window或document中使用.attr()方法在jQuery1.6中不能正常运行,因为window和document中不能有attributes。它们包含properties(比如:location或readyState),必须使用.prop()方法操作或简单地使用javascript原生的方法。在jQuery1.6.1中,window和document中使用.attr()将被自动转成使用.prop,而不是抛出一个错误。

其次,checked,selected和前面提到的其它boolean attributes,因为这些attributes和其相应的properties之间的特殊关系而被特殊对待。基本上,一个attribute就是以下html中你看到的:

 Js代码 复制代码 收藏代码

  1. <input type=”checkbox” checked=”checked”>  
<input type=”checkbox” checked=”checked”>

 boolean attributes,比如:checked,仅被设置成默认值或初始值。在一个checkbox的元素中,checked attributes在页面加载的时候就被设置,而不管checkbox元素是否被选中。

 properties就是浏览器用来记录当前值的东西。正常情况下,properties反映它们相应的attributes(如果存在的话)。但这并不是boolean attriubutes的情况。当用户点击一个checkbox元素或选中一个select元素的一个option时,boolean properties保持最新。但相应的boolean attributes是不一样的,正如上面所述,它们仅被浏览器用来保存初始值。

Js代码 复制代码 收藏代码

  1. $(“:checkbox”).get(0).checked = true;  
  2. // Is the same as $(":checkbox:first").prop(“checked”, true);  
$(“:checkbox”).get(0).checked = true;
// Is the same as $(":checkbox:first").prop(“checked”, true);

 在jQuery1.6中,如果使用下面的方法设置checked:

Js代码 复制代码 收藏代码
  1. $(“:checkbox”).attr(“checked”, true);  
$(“:checkbox”).attr(“checked”, true); 

将不会检查checkbox元素,因为它是需要被设置的property,但是你所有的设置都是初始值。 

然而,曾经jQuery1.6被释放出来的时候,jQuery团队明白当浏览器仅关心页面加载时,设置一些值不是特别的有用。所以,为了保持向后兼容性和.attr()方法的有用性,我们可以继续在jQuery1.6.1中使用.attr()方法取得和设置这些boolean attributes。

最普通的attributes是checked,selected,disabled和readOnly,但下面是jQuery1.6.1支持的使用.attr()动态地取得和设置boolean attributes/properties的完整列表:

Js代码 复制代码 收藏代码
  1. autofocus, autoplay, async, checked, controls, defer, disabled,  
  2. hidden, loop, multiple, open, readonly, required, scoped, selected  
autofocus, autoplay, async, checked, controls, defer, disabled,
hidden, loop, multiple, open, readonly, required, scoped, selected 

(译者注:大部分都是html5新增的属性)

 还是建议使用.prop()方法来设置这些boolean attributes/properties,即使这些用例没有转换成使用.prop()方法,但是你的代码仍然可以在jQuery1.6.1中正常运行。

 下面是一些attributes和properties的列表,正常情况下,应该使用其对应的方法(见下面的列表)来取得和设置它们。下面的是首用法,但是.attr()方法可以运行在所有的attributes情况下。

 注意:一些DOM元素的properties也被列在下面,但是仅运行在新的.prop()方法中

 *例如: window.location
**如果需要在(if needed over) .width()
 

.attr()和.prop()都不应该被用来取值/设值。使用.val()方法代替(即使使用.attr("value","somevalue") 可以继续运行,就像1.6之前做的那样)

3、首选用法的概述

.prop()方法应该被用来处理boolean attributes/properties以及在html(比如:window.location)中不存在的properties。其他所有的attributes(在html中你看到的那些)可以而且应该继续使用.attr()方法来进行操作。

上面的概述已经描述的够清楚了,我也没有必要再总结了。


jQuery中的prop()与attr()的用法区别

 

attr()方法

 

一般jQuery中,获取或设置属性的值是通过attr()方法。如:

 

  • 返回文档中所有图像的src属性值:$(“img”).attr(“src”);
  • 为所有图像设置src和alt属性:$(“img”).attr({ src: “test.jpg”, alt: “Test Image” });

 

prop()方法

 

而jquery的1.6版本中,增加了prop(),方法,有什么用意呢?大家都知道有的浏览器只要写disabled,checked就可以了,而有的要写成disabled = “disabled”,checked=”checked”,比如用attr(“checked”)获取checkbox的checked属性时选中的时候可以取到值,值为”checked”但没选中获取值就是undefined。

 

jq提供新的方法“prop”来获取这些属性,就是来解决这个问题的,以前我们使用attr获取checked属性时返回”checked”和undefined,现在使用prop方法获取属性则统一返回true和false。

 

那么,我们的结论是 1.添加属性名称该属性就会生效应该使用prop(); 2.是有true,false两个属性使用prop();如checked、selected、disabled 3.其他则使用attr();

 

 prop()使用方法

 

语法:prop(name|properties|key,value|fn) 获取在匹配的元素集中的第一个元素的属性值。

 

随着一些内置属性的DOM元素或window对象,如果试图将删除该属性,浏览器可能会产生错误。jQuery第一次分配undefined值的属性,而忽略了浏览器生成的任何错误

  • 选中复选框为true,没选中为false:

 

 

  • 禁用页面上的所有复选框:

 

 

  • 禁用和选中所有页面上的复选框:

 

 

  •  通过函数来设置所有页面上的复选框被选中:

 

 

 

官方建议attr(),prop()的使用表

 

Attribute/Property.attr().prop()
accesskey  
align  
async
autofocus
checked
class  
contenteditable  
draggable  
href  
id  
label  
location ( i.e. window.location )
multiple
readOnly
rel  
selected
src  
tabindex  
title  
type  
width ( if needed over .width() )

 

 

 

 

 

 

 

 

 

 

 

 

posted on   新西兰程序员  阅读(5439)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示