给元素更改样式,一般使用 addClass() 和removeClass()

jquery官方文档 对 addClass的介绍:

Adds the specified class(es) to each element in the set of matched elements.

实例代码

<!DOCTYPE html>
<html lang="en"> <head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="./WebContent/resources/common/js/jquery.js"></script>
<style>
.aaa {
color: rebeccapurple;
font-size: 4em;
} .bbb {
width: 20%;
height: 20%;
background: #f1f1a1;
}
</style>
</head> <body>
<div id="test1" class="div">
<span class="aaa bbb">hellow world
</span>
</div>
<div class="div">
<span class="aaa bbb">hellow world</span>
</div> <div>
<input type="button" value="点击我">
</div>
</body>
<script>
$('input[type=button]').on('click', e => {
console.log(e);
console.log(e.target);
console.log($(e.target));
let ee = document.getElementById('test1');
console.log(ee);
$('.bbb',ee).toggleClass('aaa');
});
    $('input',$('button2')).on('click',function () {
    // 通过this获取到当前对象
    console.log(this);
    // jquery 对象
    console.log($(this));
    // 移除样式
    $('.div span').removeClass();
})
</script>
</html>

addClass() 

给匹配的元素集合添加 指定的class样式(注意 括号中的es 可以移除多个样式)

$('.div span').addClass('aaa') ;添加指定样式

$('.div span').addClass('aaa bbb') ;添加多个样式

removeClass() 

jquery官方文档 对 removeClass的介绍:

Remove a single class, multiple classes, or all classes from each element in the set of matched elements.

给匹配的元素集合移除 一个 或者 多个 或者 所有的 class样式

$('.div span').removeClass() 方法移除所有的样式

$('.div span').removeClass('aaa') 移除指定的class 为 aaa的样式

$('.div span').removeClass('aaa bbb') 移除指定的多个样式;

 toggleClass()

toggle 是切换的意思,官方解释

Add or remove one or more classes from each element in the set of matched elements, depending on either the class’s presence or the value of the state argument.

如果 有这个class 则 移除此class,如果没有这个class,则添加上

$('.div span').toggleClass('aaa bbb');

总结

1.toggleClass 侧重点在样式指定样式的切换,一般配合点击、 鼠标悬浮 、鼠标划过事件

2.removeClass 和 addClass 重点在样式的移除

3.针对频繁切换,使用toggle,代码会简介流程很多

从调试,到验证,再些这个随笔,花了二个小时.再想想此文的技术含量,有点汗颜.继续努力.争取能写出高端,有含量的干货

一下是关于伪类和伪元素的文章链接

CSS伪类与伪元素完全指南

jquery toggleclass方法的更多相关文章

  1. jQuery的addClass,removeClass和toggleClass方法

    jQuery的addClass,removeClass和toggleClass方法,最后一个方法在某一情形之下,可以替代前面2个方法. 第一个方法addClass为元素添加一个class. 第二个方法 ...

  2. jQuery addClass removeClass toggleClass方法概述

    通过动态改变类名(class),可以让其修改元素呈现出不同的效果.在HTML结构中里,多个class以空格分隔,当一个节点(或称为一个标签)含有多个class时,DOM元素响应的className属性 ...

  3. jQuery on()方法

    jQuery on()方法是官方推荐的绑定事件的一个方法. $(selector).on(event,childSelector,data,function,map) 由此扩展开来的几个以前常见的方法 ...

  4. jQuery on()方法示例及jquery on()方法的优点

    jQuery on()方法是官方推荐的绑定事件的一个方法. $(selector).on(event,childSelector,data,function,map) 由此扩展开来的几个以前常见的方法 ...

  5. jquery ajax方法和其他api回顾

    >> $ele.load(url, data, callback); //带data参数时为POST请求,否则为GET请求 // $('#testload').load('data.htm ...

  6. jQuery 事件方法大全-超全的总结

    jquery经常使用的事件: /*     on     off     hover     blur     change     click     dblclick     focus     ...

  7. jquery的方法总结

    1.1.概述随着WEB2.0及ajax思想在互联网上的快速发展传播,陆续出现了一些优秀的Js框架,其中比较著名的有Prototype.YUI.jQuery.mootools.Bindows以及国内的J ...

  8. jQuery jQuery on()方法

    jQuery on()方法是官方推荐的绑定事件的一个方法. $(selector).on(event,childSelector,data,function,map) 由此扩展开来的几个以前常见的方法 ...

  9. jQuery css()方法及方法

    知识点一:jQuery css()方法 1.css()方法是什么? css()方法设置或返回被选元素的一个或多个样式属性. 2.返回CSS属性 1.$("p").css(" ...

随机推荐

  1. (C# Window Service) Verify that you have sufficient privileges to start system services

    写了个Windows Service, 用Wix 写了个Installer,编译通过,生成了msi 安装文件,但是安装的时候总是提示: Product: KingPro Service -- Erro ...

  2. sql处理百万级以上的数据提高查询速度的方法

    原文:http://blog.csdn.net/zhengyiluan/article/details/51671599 处理百万级以上的数据提高查询速度的方法: 1.应尽量避免在 where 子句中 ...

  3. linux day4

    echo $? :上一次命令是否执行正确make clean :清理编译垃圾文件env :查询环境变量 安装pip下载软件源cat /etc/pip.conf [global] index-url = ...

  4. Angular架构概览(截取部分)

    原文链接:https://angular.cn/docs/ts/latest/guide/architecture.html 我们是这样写 Angular 应用的:用 Angular 扩展语法编写 H ...

  5. 从golang-gin-realworld-example-app项目学写httpapi (二)

    https://github.com/gothinkster/golang-gin-realworld-example-app/blob/master/users/models.go 模型定义 use ...

  6. SQL点点滴滴_SQL分页查询

    假如tb_customer表中的数据量非常大,在显示时要分页显示而且每页只显示10条信息.为了效果我们取第三页的数据. 方法一:遍历两次表,取不同的数据. select top 10 * from t ...

  7. 面向对象进阶------>内置函数 str repr new call 方法

    __new__方法: 我们来讲个非常非常重要的内置函数和init一样重要__new__其实在实例话对象的开始  是先继承父类中的new方法再执行init的  就好比你生孩子 先要把孩子生出来才能对孩子 ...

  8. [翻译] JNWSpringAnimation

    JNWSpringAnimation https://github.com/jwilling/JNWSpringAnimation JNWSpringAnimation is a subclass o ...

  9. [翻译] KGModal

    KGModal KGModal is an easy drop in control that allows you to display any view in a modal popup. The ...

  10. 深入探索C++对象模型(1) 关于对象(思维导图)

    通过上面整个关于对象的基础知识框架,我们来分析两个例子,看一下在内存中,对象究竟长什么样.   Demo1:C++对象模型的内存布局 class Point { public: Point( floa ...