欢迎来个人专栏查看系列文章。javascript
眼看 jQuery 的源码就快到头了,后面还有几个重要的内容,包括 ajax 和动画操做,加油把它们看完,百度前端学院的新一批课程也开始了。百度前端学院。css
class 的的操做应该算是比较愉快的,由于内容不是不少,或者说,内容涉及到的原生操做不是很大,就一个 className 或 getAttribute,主要仍是来看下它涉及到的一些兼容性操做。html
先来讲一个比较有趣的 class 操做,先把连接贴上。前端
js 有一个很是大的缺陷,就是没法控制伪元素的样式,好比 after 和 before,这样子会失去不少乐趣(一样也带来了不少乐趣)。上面的连接是 stackoverflow 的解答。java
1. class 方式node
经过事先定义 class 的方式来解决:git
p:before { content: "c1" } p.click:before { content: "click" } // js $("p").on("click", function(){ $(this).toggleClass('click'); })
2. 內联 style 方式github
这种方式不优雅,也是一种解决办法。ajax
var str = "click"; $('<style>p:before{content:"' + str + '""}</style>').appendTo('head');
3. jQuery data-attr 来解决正则表达式
这种方式是依靠 content 的特性:
p:before { content: attr(data-click); } //js var str = 'click'; $("p").on("click", function(){ $(this).attr("data-click", str); })
这种方式应该是动态改变。
jQuery 的应用仍是挺普遍的。
jQuery 中的 class 操做仍是颇有意思,会用到不少正则表达式,我超喜欢正则表达式的。
若是让我用原生的 js 来实现 class 操做,我会想到两种方式,一种是 className,它的兼容性很是好,全部浏览器都支持,包括 mobile。第二个是 getAttribute,也是全部浏览器都支持(有版本限制)。
先从 hasClass 提及吧:
// 获取 class-name function getClass( elem ) { return elem.getAttribute && elem.getAttribute( "class" ) || ""; } // 将 class name 进行处理 function stripAndCollapse( value ) { var tokens = value.match( /[^\x20\t\r\n\f]+/g ) || []; return tokens.join( " " ); } jQuery.fn.extend( { hasClass: function( selector ) { var className, elem, i = 0; className = " " + selector + " "; while ( ( elem = this[ i++ ] ) ) { if ( elem.nodeType === 1 && ( " " + stripAndCollapse( getClass( elem ) ) + " " ).indexOf( className ) > -1 ) { return true; } } return false; } } );
能够看出 getClass
函数使用的是 getAttribute
方法。
接下来看一下添加 add:
jQuery.fn.extend( { addClass: function( value ) { var classes, elem, cur, curValue, clazz, j, finalValue, i = 0; // 参数为函数... if ( jQuery.isFunction( value ) ) { return this.each( function( j ) { jQuery( this ).addClass( value.call( this, j, getClass( this ) ) ); } ); } if ( typeof value === "string" && value ) { // 能够添加多个 class classes = value.match( /[^\x20\t\r\n\f]+/g ) || []; while ( ( elem = this[ i++ ] ) ) { curValue = getClass( elem ); cur = elem.nodeType === 1 && ( " " + stripAndCollapse( curValue ) + " " ); if ( cur ) { j = 0; while ( ( clazz = classes[ j++ ] ) ) { if ( cur.indexOf( " " + clazz + " " ) < 0 ) { cur += clazz + " "; } } // 在这里 set class,有个 diff 判断 finalValue = stripAndCollapse( cur ); // 去除两侧空格 if ( curValue !== finalValue ) { elem.setAttribute( "class", finalValue ); } } } } return this; } } );
jQuery 大体处理的思路是这样的:先把当前 elem 中的 class 取出来 cur
,要添加的 value
若是在 cur 中 indexOf
的值显示不存在,就在 cur 后面加上 value。
删除可能要麻烦一点点:
jQuery.fn.extend( { removeClass: function( value ) { var classes, elem, cur, curValue, clazz, j, finalValue, i = 0; // 不知道在哪里用到 value 为 function 状况 if ( jQuery.isFunction( value ) ) { return this.each( function( j ) { jQuery( this ).removeClass( value.call( this, j, getClass( this ) ) ); } ); } // 无参数表示 移除全部的 class ... if ( !arguments.length ) { return this.attr( "class", "" ); } if ( typeof value === "string" && value ) { classes = value.match( rnothtmlwhite ) || []; while ( ( elem = this[ i++ ] ) ) { curValue = getClass( elem ); // This expression is here for better compressibility (see addClass) cur = elem.nodeType === 1 && ( " " + stripAndCollapse( curValue ) + " " ); if ( cur ) { j = 0; while ( ( clazz = classes[ j++ ] ) ) { // 移除全部须要移除的 class while ( cur.indexOf( " " + clazz + " " ) > -1 ) { cur = cur.replace( " " + clazz + " ", " " ); } } // Only assign if different to avoid unneeded rendering. finalValue = stripAndCollapse( cur ); if ( curValue !== finalValue ) { elem.setAttribute( "class", finalValue ); } } } } return this; } } );
能够看出 remove 的操做基本上和 add 同样,只不过处理 class 的时候略有不一样:
// 这里用 while,是有技巧的 while ( cur.indexOf( " " + clazz + " " ) > -1 ) { cur = cur.replace( " " + clazz + " ", " " ); }
用 replace 替换匹配的 clazz 为空格。
toggleClass 使用的频率也比较高。
先来看看大体用法,你确定会忽略它的第二个参数的意思。.toggleClass(),当第二个参数为 true 的状况,就是 addClass,为 false 时,removeClass,从源码来看,就是直接调用的这两个函数。
除了两个参数,还有无参和只有 false 状况,下面也都有明确的处理办法。
jQuery.fn.extend( { toggleClass: function( value, stateVal ) { var type = typeof value; // 第二个参数为 boolean if ( typeof stateVal === "boolean" && type === "string" ) { return stateVal ? this.addClass( value ) : this.removeClass( value ); } if ( jQuery.isFunction( value ) ) { return this.each( function( i ) { jQuery( this ).toggleClass( value.call( this, i, getClass( this ), stateVal ), stateVal ); } ); } return this.each( function() { var className, i, self, classNames; if ( type === "string" ) { // Toggle individual class names i = 0; self = jQuery( this ); classNames = value.match( rnothtmlwhite ) || []; while ( ( className = classNames[ i++ ] ) ) { // 有则删,无则加,逻辑很简单 if ( self.hasClass( className ) ) { self.removeClass( className ); } else { self.addClass( className ); } } // 当无参或只有一个 false 时,全部 class 都执行 } else if ( value === undefined || type === "boolean" ) { className = getClass( this ); if ( className ) { // Store className if set dataPriv.set( this, "__className__", className ); } if ( this.setAttribute ) { this.setAttribute( "class", className || value === false ? "" : dataPriv.get( this, "__className__" ) || "" ); } } } ); } } );
看得出来,这个逻辑和前面两个很像,不过当无参或只有一个 boolean 且 false 时,先将当前的 className 保存到 data cache 中,而后实现 toggle 操做:
if ( this.setAttribute ) { this.setAttribute( "class", className || value === false ? // 判断条件 "" : // 有则设空 dataPriv.get( this, "__className__" ) || "" // 无则从 data cache 取 ); }
感受 jQuery 中的 class 操做不是很复杂,难道是我在进步吗,哈哈。
jQuery 2.0.3 源码分析 样式操做
Selecting and manipulating CSS ..
.toggleClass()
本文在 github 上的源码地址,欢迎来 star。
欢迎来个人博客交流。