所以我有一个 toggleClass
来在按下 div 时更改某些 div 的类(它更改背景)。
当按下另一个 div 时,我需要将 bg 变回正常,并关闭 ".subtitle"
,然后切换您按下的新 div。
目前,关闭和打开功能正常,但背景没有变化。
标准类是.title
,我还有一个.title.close
,它只包含其他背景。
我可以在这里得到一些帮助吗?
CSS:
.title: contains the normal bg
.title.open: contains the "open" bg
.title.close: contains the bg of title(the normal bg)
jQuery
$(document).ready(function()
{
$(".title").click(function()
{
var $element = $(this);
//What I've tried
$('open').toggleClass('');
$('.subtitle').hide(500);
//This is changing the bg, and show the ".subtitle" you've pressed
$element.toggleClass('open');
$element.children('.subtitle').toggle(500);
});
});
最佳答案
当你不想切换,但特别想添加或删除一个类而不考虑当前状态时,你可以只使用 .addClass()
或 .removeClass()
甚至清除所有类。一些例子:
// remove the "open" class from a particular element
$element.removeClass("open");
// remove the "open" class from all elements that have it
$(".open").removeClass("open");
关于jQuery toggleClass - 回到普通类(class)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15266999/