jQuery attr() 源码解读
我们知道,$().attr()实质上是内部调用了jQuery.access方法,在调用时jQuery.attr作为回调传入。在通过种种判断(参看jQuery.access()方法)之后,取值和赋值最后调用了这个jQuery.attr方法。
所以,关键是看jQuery.attr这里怎么走了~~
源码如下:
attr: function( elem, name, value ) {
var hooks, ret,
nType = elem.nodeType;
//如果elem不存在,或者是文本、注释、属性节点
// don't get/set attributes on text, comment and attribute nodes
if ( !elem || nType === 3 || nType === 8 || nType === 2 ) {
return;
}
// Fallback to prop when attributes are not supported
if ( typeof elem.getAttribute === core_strundefined ) {//如果elem不支持getAttribute 比如document或者文档碎片
return jQuery.prop( elem, name, value );//调用jQuery.prop方法
}
// All attributes are lowercase
// Grab necessary hook if one is defined
//?
if ( nType !== 1 || !jQuery.isXMLDoc( elem ) ) {//elem不是标签或者elem不在xml中
name = name.toLowerCase();//属性名大写
hooks = jQuery.attrHooks[ name ] ||
( jQuery.expr.match.bool.test( name ) ? boolHook : nodeHook );//jQuery.attrHooks.type
}
if ( value !== undefined ) {//赋值
if ( value === null ) {//如果设的值为null,相当于移除attr ,比如.attr('checked',null);
jQuery.removeAttr( elem, name );
} else if ( hooks && "set" in hooks && (ret = hooks.set( elem, value, name )) !== undefined ) {//调用钩子的set方法
return ret;
} else {
elem.setAttribute( name, value + "" );//如果value是数值,隐式转为字符串
return value;
}
} else if ( hooks && "get" in hooks && (ret = hooks.get( elem, name )) !== null ) {//调用钩子的get
return ret;
} else {//取值
ret = jQuery.find.attr( elem, name );//调用Sizzle.attr 因为jquery中有一句:jQuery.find = Sizzle;
//将null值修正为undefined
// Non-existent attributes return null, we normalize to undefined
return ret == null ?
undefined :
ret;
}
}
从上面的代码看,有7个走向:
1、return
2、jQuery.prop
3、jQuery.removeAttr
4、attrHooks.set
5、elem.setAttribute
6、attrHooks.get
7、Sizzle.attr
前面5个都是设置值,6和7是读值。
2和3以后会读到,这里不提,先看看Sizzle.attr中是怎么一回事吧^^
Sizzle.attr = function( elem, name ) {
// Set document vars if needed
if ( ( elem.ownerDocument || elem ) !== document ) {//如果不是当前document
setDocument( elem );//不知道干嘛 考虑iframe的情况?
}
//Expr.attrHandle是一个对象,包含了
//async autofocus autoplay checked controls defer disabled hidden ismap loop
//multiple open readonly required scoped selected 等自定义方法(属性)
var fn = Expr.attrHandle[ name.toLowerCase() ],
// Don't get fooled by Object.prototype properties (jQuery #13807)
//hasOwn 就是hasOwnProperty
//所以这个hasOwn.call( Expr.attrHandle, name.toLowerCase() ) 是在判断传入的属性名是不是这个对象的
//自定义属性,而不是原型上的属性
//val为调用自定义方法的结果或者undefined
val = fn && hasOwn.call( Expr.attrHandle, name.toLowerCase() ) ?
fn( elem, name, !documentIsHTML ) :
undefined;
//如果val为undefined
//如果(support.attributes为真或者document不是html),返回elem.getAttribute( name )的值
//否则 val为 elem.getAttributeNode(name))的值
//如果val为真,并且val.specified为真,返回val.value
//否则返回null
//否则返回val
return val === undefined ?
support.attributes || !documentIsHTML ?
elem.getAttribute( name ) :
(val = elem.getAttributeNode(name)) && val.specified ?
val.value :
null :
val;
};
复杂的三元运算符看得头都大了有木有,好吧,这里能看到,取值用getAttribute或者getAttributeNode。
再看attrHooks是一些什么内容:
attrHooks: {
type: {
set: function( elem, value ) {
if ( !jQuery.support.radioValue && value === "radio" && jQuery.nodeName(elem, "input") ) {
// Setting the type on a radio button after the value resets the value in IE6-9
// Reset value to default in case type is set after value during creationvar val = elem.value;
elem.setAttribute( "type", value );
if ( val ) {
elem.value = val;
}
return value;
}
}
}
}
从这里可以看到,attrHooks与设置input标签的type值有关,恩,如果设置的值为radio的时候,注意下。
jQuery attr() 源码解读的更多相关文章
- jQuery.Callbacks 源码解读二
一.参数标记 /* * once: 确保回调列表仅只fire一次 * unique: 在执行add操作中,确保回调列表中不存在重复的回调 * stopOnFalse: 当执行回调返回值为false,则 ...
- jQuery toggleClass 源码解读
toggleClass: function( value, stateVal ) { var type = typeof value;//值类型 if ( typeof stateVal === &q ...
- jQuery removeAttr() 源码解读
removeAttr比attr的代码要简单很多~~~ removeAttr: function( name ) { return this.each(function() { jQuery.remov ...
- jquery.fileupload源码解读笔记
基础编程风格 新建 test.html 和 test.js和 main.js和 无论哪种顺序 <body> <script src="/Sandeep/js/jquery ...
- jQuery.extend()源码解读
// extend方法为jQuery对象和init对象的prototype扩展方法// 同时具有独立的扩展普通对象的功能jQuery.extend = jQuery.fn.extend = funct ...
- jQuery框架源码解读
1.jQuery 1.9.1 parseJSON: function( data ) { // Attempt to parse using the native JSON parser first ...
- jQuery position() 源码解读
position的代码比较简单... position: function() { if ( !this[ 0 ] ) { return; } var offsetParent, offset, el ...
- jquery offsetParent()源码解读
offsetParent: function() { return this.map(function() { var offsetParent = this.offsetParent || docE ...
- jQuery addClass() 源码解读
addClass: function( value ) { var classes, elem, cur, clazz, j, i = 0, len = this.length, proceed = ...
随机推荐
- debian配置集锦
1 关闭蜂鸣 在/etc/bash.bashrc中加入下面的行: setterm -blength=0 2 debian bash路径显示太长 将.bashrc中的 else PS1='${debia ...
- SPOJ:House Fence(分治&DP)
"Holiday is coming, holiday is coming, hurray hurray!" shouts Joke in the last day of his ...
- [SHOI 2009] 会场预约
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=2028 [算法] 直接用std :: set维护即可 时间复杂度 : O(NlogN) ...
- Watir: 如何处理简单的网页弹出警告框?
以下是一个很经典的把Watir与AutoIt连接在一起的实例.如果我们对AutoIT了解的更多,处理类似的问题会更加简单.以下实例会判断页面上是否有某“删除”链接,一旦有该链接,就点击,然后点击弹出的 ...
- bzoj1037生日聚会——DP
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1037 记录每个状态时前面所有连续子序列中男生与女生差距的最大值,根据那个转移即可. 代码如下 ...
- 六个优雅的 Linux 命令行技巧
一些非常有用的命令能让命令行的生活更满足,使用 Linux 命令工作可以获得许多乐趣,但是如果您使用一些命令,它们可以减少您的工作或以有趣的方式显示信息时,您将获得更多的乐趣.在今天的文章中,我们将介 ...
- CodeForces - 504A && CodeForces - 624C && CodeForces - 2B
Points 1. 关键要看到以度数为1的点作为突破口. 2. 关键是发现两者不同只能是a-c,而剩余的点必须为b 3. 注意0的情况.
- python __builtins__ classmethod类 (11)
11.'classmethod', 修饰符对应的函数不需要实例化,不需要 self 参数,但第一个参数需要是表示自身类的 cls 参数,可以来调用类的属性,类的方法,实例化对象等. class cla ...
- hdu 3038 How Many Answers Are Wrong【带权并查集】
带权并查集,设f[x]为x的父亲,s[x]为sum[x]-sum[fx],路径压缩的时候记得改s #include<iostream> #include<cstdio> usi ...
- bzoj1485: [HNOI2009]有趣的数列(Catalan数)
1485: [HNOI2009]有趣的数列 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 2105 Solved: 1117[Submit][Stat ...