有人可以帮我解决这个问题吗?
基本上,我希望脚本在单击时将“active”类添加到“li”,并在另一次单击时将其删除 - 好吧,它添加该类并自动删除它:-) 我真的很感激任何解决方案。
jQuery(document).ready(function() {
jQuery('li').each(function() {
var faq = jQuery(this), state = true, answer = faq.next('.answer').hide().css('height','auto').slideUp();
faq.click(function() {
state = !state;
answer.slideToggle(state);
faq.addClass('active',state);
if(jQuery("li").hasClass("active")) {
jQuery(".active").remove(); }
});
});
});
jQuery(".active").removeClass(); }
解决了一半的问题
最佳答案
实际上是.removeClass()你必须尝试..!!
jQuery(document).ready(function() {
jQuery('li').each(function() {
var faq = jQuery(this), state = true, answer = faq.next('.answer').hide().css('height','auto').slideUp();
faq.click(function() {
state = !state;
answer.slideToggle(state);
faq.addClass('active',state);
if(jQuery("li").hasClass("active")) {
jQuery(".active").removeClass(); }
});
});
});
或
使用
jQuery(document).ready(function() {
jQuery('li').each(function() {
var faq = jQuery(this), state = true, answer = faq.next('.answer').hide().css('height','auto').slideUp();
faq.click(function() {
state = !state;
answer.slideToggle(state);
faq.addClass('active',state);
$( this ).toggleClass( "active" );
});
});
});
更新
根据@AndyAndy的评论更新:
imagine that
has a "+" icon, and on click it changes to "-" icon and answer shows up.
jQuery(document).ready(function() {
jQuery('li').each(function() {
var faq = jQuery(this), answer = faq.next('.answer').hide().css('height','auto').slideUp();
faq.click(function() {
answer.slideToggle('.active');
if($(this).text()=='+')
{
$(this).text('-');
}
else{
$(this).text('+');
}
});
});
});
关于javascript - 删除 jquery 类时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23459563/