码迷,mamicode.com
首页 > Web开发 > 详细

jquery attr() & prop()

时间:2015-08-11 21:11:07      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:

version: 1.9.1

Attr

 1 attr: function( elem, name, value ) {
 2     var hooks, notxml, ret,
 3         nType = elem.nodeType;
 4 
 5     // don‘t get/set attributes on text, comment and attribute nodes
 6     if ( !elem || nType === 3 || nType === 8 || nType === 2 ) {
 7         return;
 8     }
 9 
10     // Fallback to prop when attributes are not supported
11     if ( typeof elem.getAttribute === core_strundefined ) {
12         return jQuery.prop( elem, name, value );
13     }
14 
15     notxml = nType !== 1 || !jQuery.isXMLDoc( elem );
16 
17     // All attributes are lowercase
18     // Grab necessary hook if one is defined
19     if ( notxml ) {
20         name = name.toLowerCase();
21         hooks = jQuery.attrHooks[ name ] || ( rboolean.test( name ) ? boolHook : nodeHook );
22     }
23 
24     if ( value !== undefined ) {
25 
26         if ( value === null ) {
27             jQuery.removeAttr( elem, name );
28 
29         } else if ( hooks && notxml && "set" in hooks && (ret = hooks.set( elem, value, name )) !== undefined ) {
30             return ret;
31 
32         } else {
33             elem.setAttribute( name, value + "" );
34             return value;
35         }
36 
37     } else if ( hooks && notxml && "get" in hooks && (ret = hooks.get( elem, name )) !== null ) {
38         return ret;
39 
40     } else {
41 
42         // In IE9+, Flash objects don‘t have .getAttribute (#12945)
43         // Support: IE9+
44         if ( typeof elem.getAttribute !== core_strundefined ) {
45             ret =  elem.getAttribute( name );
46         }
47 
48         // Non-existent attributes return null, we normalize to undefined
49         return ret == null ?
50             undefined :
51             ret;
52     }
53 }

 

Prop

 1 prop: function( elem, name, value ) {
 2     var ret, hooks, notxml,
 3         nType = elem.nodeType;
 4 
 5     // don‘t get/set properties on text, comment and attribute nodes
 6     if ( !elem || nType === 3 || nType === 8 || nType === 2 ) {
 7         return;
 8     }
 9 
10     notxml = nType !== 1 || !jQuery.isXMLDoc( elem );
11 
12     if ( notxml ) {
13         // Fix name and attach hooks
14         name = jQuery.propFix[ name ] || name;
15         hooks = jQuery.propHooks[ name ];
16     }
17 
18     if ( value !== undefined ) {
19         if ( hooks && "set" in hooks && (ret = hooks.set( elem, value, name )) !== undefined ) {
20             return ret;
21 
22         } else {
23             return ( elem[ name ] = value );
24         }
25 
26     } else {
27         if ( hooks && "get" in hooks && (ret = hooks.get( elem, name )) !== null ) {
28             return ret;
29 
30         } else {
31             return elem[ name ];
32         }
33     }
34 }

 

参考:http://wenzhixin.net.cn/2013/05/24/jquery_attr_prop

 

jquery attr() & prop()

标签:

原文地址:http://www.cnblogs.com/hemi/p/4722098.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!