gpt4 book ai didi

javascript - 单击元素并使用 jQuery 设置为 'Active'

转载 作者:太空宇宙 更新时间:2023-11-04 01:22:17 24 4
gpt4 key购买 nike

我有一个显示 6 个框的简单脚本。

计划是当用户点击这些框中的任何一个时,它们应该变成灰色背景。但是,这不起作用。我使用 .on 的方式有问题吗?

演示:http://jsfiddle.net/szzqe8L1/

$(".thumb-overlay-content").on('click',function() {
$(this).removeClass('rotateOut').addClass('rotateIn');
}, function() {
$(this).removeClass('rotateIn').addClass('rotateOut');
});
.thumb-overlay-content { margin:10px; background:#CCC; float:left; text-align:center; padding:50px; }
.animated { background:red; }
.rotateOut { border:3px solid blue; }
.rotateIn { border:3px solid green; background:#ccc; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="thumb-overlay-content animated rotateOut">Box1</div>
<div class="thumb-overlay-content animated rotateOut">Box1</div>
<div class="thumb-overlay-content animated rotateOut">Box1</div>
<div class="thumb-overlay-content animated rotateOut">Box1</div>
<div class="thumb-overlay-content animated rotateOut">Box1</div>
<div class="thumb-overlay-content animated rotateOut">Box1</div>
<div class="thumb-overlay-content animated rotateOut">Box1</div>

最佳答案

.on('click' 需要一个回调函数,它说明当用户点击与选择器匹配的元素时要做什么。

您可以使用 .toggleClass() 获得您想要的功能

$(".thumb-overlay-content").on('click',function() {
$(this).toggleClass('rotateOut rotateIn');
});
.thumb-overlay-content { margin:10px; background:#CCC; float:left; text-align:center; padding:50px; }
.animated { background:red; }
.rotateOut { border:3px solid blue; }
.rotateIn { border:3px solid green; background:#ccc; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="thumb-overlay-content animated rotateOut">Box1</div>
<div class="thumb-overlay-content animated rotateOut">Box1</div>
<div class="thumb-overlay-content animated rotateOut">Box1</div>
<div class="thumb-overlay-content animated rotateOut">Box1</div>
<div class="thumb-overlay-content animated rotateOut">Box1</div>
<div class="thumb-overlay-content animated rotateOut">Box1</div>
<div class="thumb-overlay-content animated rotateOut">Box1</div>

关于javascript - 单击元素并使用 jQuery 设置为 'Active',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48804024/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com