资讯专栏INFORMATION COLUMN

jQuery 源码系列(十八)class 相关操作

BingqiChen / 2988人阅读

摘要:眼看的源码就快到头了,后面还有几个重要的内容,包括和动画操作,加油把它们看完,百度前端学院的新一批课程也开始了。,当第二个参数为的情况,就是,为时,,从源码来看,就是直接调用的这两个函数。参考源码分析样式操作本文在上的源码地址,欢迎来。

欢迎来我的专栏查看系列文章。

眼看 jQuery 的源码就快到头了,后面还有几个重要的内容,包括 ajax 和动画操作,加油把它们看完,百度前端学院的新一批课程也开始了。百度前端学院。

class 的的操作应该算是比较愉快的,因为内容不是很多,或者说,内容涉及到的原生操作不是很大,就一个 className 或 getAttribute,主要还是来看下它涉及到的一些兼容性操作。

class 操作

先来说一个比较有趣的 class 操作,先把链接贴上。

js 有一个非常大的缺陷,就是无法控制伪元素的样式,比如 after 和 before,这样子会失去很多乐趣(同样也带来了很多乐趣)。上面的链接是 stackoverflow 的解答。

1. class 方式

通过事先定义 class 的方式来解决:

</>复制代码

  1. p:before {
  2. content: "c1"
  3. }
  4. p.click:before {
  5. content: "click"
  6. }
  7. // js
  8. $("p").on("click", function(){
  9. $(this).toggleClass("click");
  10. })

2. 內联 style 方式

这种方式不优雅,也是一种解决办法。

</>复制代码

  1. var str = "click";
  2. $("").appendTo("head");

3. jQuery data-attr 来解决

这种方式是依靠 content 的特性:

</>复制代码

  1. p:before {
  2. content: attr(data-click);
  3. }
  4. //js
  5. var str = "click";
  6. $("p").on("click", function(){
  7. $(this).attr("data-click", str);
  8. })

这种方式应该是动态改变。

jQuery 的应用还是挺广泛的。

fn.hasClass

jQuery 中的 class 操作还是很有意思,会用到很多正则表达式,我超喜欢正则表达式的。

如果让我用原生的 js 来实现 class 操作,我会想到两种方式,一种是 className,它的兼容性非常好,所有浏览器都支持,包括 mobile。第二个是 getAttribute,也是所有浏览器都支持(有版本限制)。

先从 hasClass 说起吧:

</>复制代码

  1. // 获取 class-name
  2. function getClass( elem ) {
  3. return elem.getAttribute && elem.getAttribute( "class" ) || "";
  4. }
  5. //class name 进行处理
  6. function stripAndCollapse( value ) {
  7. var tokens = value.match( /[^x20
  8. f]+/g ) || [];
  9. return tokens.join( " " );
  10. }
  11. jQuery.fn.extend( {
  12. hasClass: function( selector ) {
  13. var className, elem,
  14. i = 0;
  15. className = " " + selector + " ";
  16. while ( ( elem = this[ i++ ] ) ) {
  17. if ( elem.nodeType === 1 &&
  18. ( " " + stripAndCollapse( getClass( elem ) ) + " " ).indexOf( className ) > -1 ) {
  19. return true;
  20. }
  21. }
  22. return false;
  23. }
  24. } );

可以看出 getClass 函数使用的是 getAttribute 方法。

fn.addClass

接下来看一下添加 add:

</>复制代码

  1. jQuery.fn.extend( {
  2. addClass: function( value ) {
  3. var classes, elem, cur, curValue, clazz, j, finalValue,
  4. i = 0;
  5. // 参数为函数...
  6. if ( jQuery.isFunction( value ) ) {
  7. return this.each( function( j ) {
  8. jQuery( this ).addClass( value.call( this, j, getClass( this ) ) );
  9. } );
  10. }
  11. if ( typeof value === "string" && value ) {
  12. // 可以添加多个 class
  13. classes = value.match( /[^x20
  14. f]+/g ) || [];
  15. while ( ( elem = this[ i++ ] ) ) {
  16. curValue = getClass( elem );
  17. cur = elem.nodeType === 1 && ( " " + stripAndCollapse( curValue ) + " " );
  18. if ( cur ) {
  19. j = 0;
  20. while ( ( clazz = classes[ j++ ] ) ) {
  21. if ( cur.indexOf( " " + clazz + " " ) < 0 ) {
  22. cur += clazz + " ";
  23. }
  24. }
  25. // 在这里 set class,有个 diff 判断
  26. finalValue = stripAndCollapse( cur ); // 去除两侧空格
  27. if ( curValue !== finalValue ) {
  28. elem.setAttribute( "class", finalValue );
  29. }
  30. }
  31. }
  32. }
  33. return this;
  34. }
  35. } );

jQuery 大致处理的思路是这样的:先把当前 elem 中的 class 取出来 cur,要添加的 value 如果在 cur 中 indexOf 的值显示不存在,就在 cur 后面加上 value。

fn.removeClass

删除可能要麻烦一点点:

</>复制代码

  1. jQuery.fn.extend( {
  2. removeClass: function( value ) {
  3. var classes, elem, cur, curValue, clazz, j, finalValue,
  4. i = 0;
  5. // 不知道在哪里用到 value 为 function 情况
  6. if ( jQuery.isFunction( value ) ) {
  7. return this.each( function( j ) {
  8. jQuery( this ).removeClass( value.call( this, j, getClass( this ) ) );
  9. } );
  10. }
  11. // 无参数表示 移除所有的 class ...
  12. if ( !arguments.length ) {
  13. return this.attr( "class", "" );
  14. }
  15. if ( typeof value === "string" && value ) {
  16. classes = value.match( rnothtmlwhite ) || [];
  17. while ( ( elem = this[ i++ ] ) ) {
  18. curValue = getClass( elem );
  19. // This expression is here for better compressibility (see addClass)
  20. cur = elem.nodeType === 1 && ( " " + stripAndCollapse( curValue ) + " " );
  21. if ( cur ) {
  22. j = 0;
  23. while ( ( clazz = classes[ j++ ] ) ) {
  24. // 移除所有需要移除的 class
  25. while ( cur.indexOf( " " + clazz + " " ) > -1 ) {
  26. cur = cur.replace( " " + clazz + " ", " " );
  27. }
  28. }
  29. // Only assign if different to avoid unneeded rendering.
  30. finalValue = stripAndCollapse( cur );
  31. if ( curValue !== finalValue ) {
  32. elem.setAttribute( "class", finalValue );
  33. }
  34. }
  35. }
  36. }
  37. return this;
  38. }
  39. } );

可以看出 remove 的操作基本上和 add 一样,只不过处理 class 的时候略有不同:

</>复制代码

  1. // 这里用 while,是有技巧的
  2. while ( cur.indexOf( " " + clazz + " " ) > -1 ) {
  3. cur = cur.replace( " " + clazz + " ", " " );
  4. }

用 replace 替换匹配的 clazz 为空格。

fn.toggleClass

toggleClass 使用的频率也比较高。

先来看看大致用法,你肯定会忽略它的第二个参数的意思。.toggleClass(),当第二个参数为 true 的情况,就是 addClass,为 false 时,removeClass,从源码来看,就是直接调用的这两个函数。

除了两个参数,还有无参和只有 false 情况,下面也都有明确的处理办法。

</>复制代码

  1. jQuery.fn.extend( {
  2. toggleClass: function( value, stateVal ) {
  3. var type = typeof value;
  4. // 第二个参数为 boolean
  5. if ( typeof stateVal === "boolean" && type === "string" ) {
  6. return stateVal ? this.addClass( value ) : this.removeClass( value );
  7. }
  8. if ( jQuery.isFunction( value ) ) {
  9. return this.each( function( i ) {
  10. jQuery( this ).toggleClass(
  11. value.call( this, i, getClass( this ), stateVal ),
  12. stateVal
  13. );
  14. } );
  15. }
  16. return this.each( function() {
  17. var className, i, self, classNames;
  18. if ( type === "string" ) {
  19. // Toggle individual class names
  20. i = 0;
  21. self = jQuery( this );
  22. classNames = value.match( rnothtmlwhite ) || [];
  23. while ( ( className = classNames[ i++ ] ) ) {
  24. // 有则删,无则加,逻辑很简单
  25. if ( self.hasClass( className ) ) {
  26. self.removeClass( className );
  27. } else {
  28. self.addClass( className );
  29. }
  30. }
  31. // 当无参或只有一个 false 时,所有 class 都执行
  32. } else if ( value === undefined || type === "boolean" ) {
  33. className = getClass( this );
  34. if ( className ) {
  35. // Store className if set
  36. dataPriv.set( this, "__className__", className );
  37. }
  38. if ( this.setAttribute ) {
  39. this.setAttribute( "class",
  40. className || value === false ?
  41. "" :
  42. dataPriv.get( this, "__className__" ) || ""
  43. );
  44. }
  45. }
  46. } );
  47. }
  48. } );

看得出来,这个逻辑和前面两个很像,不过当无参或只有一个 boolean 且 false 时,先将当前的 className 保存到 data cache 中,然后实现 toggle 操作:

</>复制代码

  1. if ( this.setAttribute ) {
  2. this.setAttribute( "class",
  3. className || value === false ? // 判断条件
  4. "" : // 有则设空
  5. dataPriv.get( this, "__className__" ) || "" // 无则从 data cache 取
  6. );
  7. }
总结

感觉 jQuery 中的 class 操作不是很复杂,难道是我在进步吗,哈哈。

参考

</>复制代码

  1. jQuery 2.0.3 源码分析 样式操作
    Selecting and manipulating CSS ..
    .toggleClass()

本文在 github 上的源码地址,欢迎来 star。

欢迎来我的博客交流。

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/86827.html

相关文章

  • dubbo源码解析(四十八)异步化改造

    摘要:大揭秘异步化改造目标从源码的角度分析的新特性中对于异步化的改造原理。看源码解析四十六消费端发送请求过程讲到的十四的,在以前的逻辑会直接在方法中根据配置区分同步异步单向调用。改为关于可以参考源码解析十远程通信层的六。 2.7大揭秘——异步化改造 目标:从源码的角度分析2.7的新特性中对于异步化的改造原理。 前言 dubbo中提供了很多类型的协议,关于协议的系列可以查看下面的文章: du...

    lijinke666 评论0 收藏0
  • dubbo源码解析(二十八)远程调用——memcached协议

    摘要:源码分析一该类继承,是协议实现的核心。属性默认端口号不支持服务暴露可以看到,服务暴露方法直接抛出异常。后记该部分相关的源码解析地址该文章讲解了远程调用中关于协议实现的部分,逻辑比较简单。 远程调用——memcached协议 目标:介绍memcached协议的设计和实现,介绍dubbo-rpc-memcached的源码。 前言 dubbo实现memcached协议是基于Memcached...

    Faremax 评论0 收藏0
  • 第二十八章:SpringBoot使用AutoConfiguration自定义Starter

    摘要:代码如下所示自定义业务实现恒宇少年码云消息内容是否显示消息内容,我们内的代码比较简单,根据属性参数进行返回格式化后的字符串。 在我们学习SpringBoot时都已经了解到starter是SpringBoot的核心组成部分,SpringBoot为我们提供了尽可能完善的封装,提供了一系列的自动化配置的starter插件,我们在使用spring-boot-starter-web时只需要在po...

    fasss 评论0 收藏0
  • JavaScript专题系列文章

    摘要:专题系列共计篇,主要研究日常开发中一些功能点的实现,比如防抖节流去重类型判断拷贝最值扁平柯里递归乱序排序等,特点是研究专题之函数组合专题系列第十六篇,讲解函数组合,并且使用柯里化和函数组合实现模式需求我们需要写一个函数,输入,返回。 JavaScript 专题之从零实现 jQuery 的 extend JavaScritp 专题系列第七篇,讲解如何从零实现一个 jQuery 的 ext...

    Maxiye 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<