我有 30 个左右的按钮,后面跟着一个div
包含内容的按钮。问题是当我单击任何按钮时它们都会触发。我希望该功能仅在单击的按钮及其对应的div
. 我想我会需要this
,next
但我不知道该怎么做。
$(function(){
$('.button').click(toggleBox);
});
function toggleBox(){
var box = $('.content');
if (box.data('on') == true){
boxOff();
} else { // box's on property is false (it's off), so we need to turn it on
boxOn();
}
}
function boxOn(){ //function removes open button and shows content
$('.button').removeClass('open-button').addClass('closed-button');
$('.content').removeClass('closed-content').addClass('open-content').data("on",true);
}
function boxOff(){ //function removes close button and hides content
$('.button').removeClass('closed-button').addClass('open-button');
$('.content').removeClass('open-content').addClass('closed-content').data("on",false);
}