首页 / 知识
关于.toggleClass()的实例分享
2023-04-11 13:31:00

如果匹配元素的父级元素有bar样式类名,这个例子将为<div class="foo">元素切换 happy 样式类; 否则他将切换 sad 样式类 。
例子:
Example: 当点击段落的是有切换 'highlight' 样式类
<!DOCTYPE html>
<html>
<head>
<style>
p { margin: 4px; font-size:16px; font-weight:bolder;
cursor:pointer; }
.blue { color:blue; }
.highlight { background:yellow; }
</style>
<script src="https://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<p class="blue">Click to toggle</p>
<p class="blue highlight">highlight</p>
<p class="blue">on these</p>
<p class="blue">paragraphs</p>
<script>
$("p").click(function () {
$(this).toggleClass("highlight");
});
</script>
</body>
</html>
Example: 每当第三次点击段落的时候添加 "highlight" 样式类, 第一次和第二次点击的时候移除 "highlight" 样式类
<!DOCTYPE html>
<html>
<head>
<style>
p { margin: 4px; font-size:16px; font-weight:bolder;
cursor:pointer; }
.blue { color:blue; }
.highlight { background:red; }
</style>
<script src="https://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<p class="blue">Click to toggle (<span>clicks: 0</span>)</p>
<p class="blue highlight">highlight (<span>clicks: 0</span>)</p>
<p class="blue">on these (<span>clicks: 0</span>)</p>
<p class="blue">paragraphs (<span>clicks: 0</span>)</p>
<script>
var count = 0;
$("p").each(function() {
var $thisParagraph = $(this);
var count = 0;
$thisParagraph.click(function() {
count++;
$thisParagraph.find("span").text('clicks: ' + count);
$thisParagraph.toggleClass("highlight", count % 3 == 0);
});
});
</script>
</body>
</html>
Example: Toggle the class name(s) indicated on the buttons for each div.
<!DOCTYPE html>
<html>
<head>
<style>
.wrap > div { float: left; width: 100px; margin: 1em 1em 0 0;
padding=left: 3px; border: 1px solid #abc; }
div.a { background-color: aqua; }
div.b { background-color: burlywood; }
div.c { background-color: cornsilk; }
</style>
<script src="https://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<div class="buttons">
<button>toggle</button>
<button class="a">toggle a</button>
<button class="a b">toggle a b</button>
<button class="a b c">toggle a b c</button>
<a href="#">reset</a>
</div>
<div class="wrap">
<div></div>
<div class="b"></div>
<div class="a b"></div>
<div class="a c"></div>
</div>
<script>
var cls = ['', 'a', 'a b', 'a b c'];
var divs = $('div.wrap').children();
var appendClass = function() {
divs.append(function() {
return '<div>' + (this.className || 'none') + '</div>';
});
};
appendClass();
$('button').bind('click', function() {
var tc = this.className || undefined;
divs.toggleClass(tc);
appendClass();
});
$('a').bind('click', function(event) {
event.preventDefault();
divs.empty().each(function(i) {
this.className = cls[i];
});
appendClass();
});
</script>
</body>
</html>
|
最新内容
相关内容
python一个数的平方怎么表示
python一个数的平方怎么表示,培训,平方根,个数,函数,模块,表示,方法,表达式,实例,下面,用python进行数学计算,可以使用python内置的函数模块,下python如何求一个数的平方
python如何求一个数的平方,培训,平方根,个数,整数,函数,模块,方法,小数,表达式,数值,用python进行数学计算,可以使用python内置的函数模块,下面python判断元素是否是字符串类型
python判断元素是否是字符串类型,培训,异常,类型,实例,字符串,数据类型,对象,元素,参数,序列,isinstance()是Python中的一个内建函数语法:isinpython如何匹配中文
python如何匹配中文,培训,中文,正则,大学,字符,范围,形式,以上,版本,结果,python中文字符的编码范围是:\u4e00-\u9fa5使用正则匹配中文#-*-cpythonpython是什么类型的语言
pythonpython是什么类型的语言,培训,系统,教育,软件,电脑,代码,简介,人工智能,类型,语言,python是一种面向对象、解释型、动态类型计算机程序python哪个版本是免费的
python哪个版本是免费的,培训,发行,通用,项目,设计,版本,以上,语言,计算机,构架,python是免费的,也就是开源的。目前市面上主流的版本有pythonpython判断字符串是否为小数的方法
python判断字符串是否为小数的方法,培训,代码,合法,小数点,小数,整数,字符串,方法,右边,左边,python想判断一个字符串是不是一个合法的小数,但python怎么求一个数的平方
python怎么求一个数的平方,培训,平方根,个数,函数,模块,方法,表达式,实例,下面,以上,用python进行数学计算,可以使用python内置的函数模块,下面python生成不重复的随机数怎么写
python生成不重复的随机数怎么写,培训,数字,范围内,结果,表达式,语法,个数,模块,以上,表示,python中的random模块用于生成随机数,其语法表达式python如何进行类型转换的
python如何进行类型转换的,培训,数字,类型,字符串,整数,函数,对象,和数,变量,字符,python提供了将变量或值从一种类型转换成另一种类型的内置python函数的高级使用方法
python函数的高级使用方法,代码,函数,灵活,数据,培训,时间,定义,程序,赋值,变量,Python的函数是“一等公民”,因此函数本身也是一个对象,函数既python一般用什么版本的
python一般用什么版本的,培训,发行,数据,基础,版本,初学者,目的,用户,大势所趋,由此可见,目前,根据数据显示,使用Python2.xd开发者站63.7%,而使