我的问题是,当我点击一个div"触发"每个div" group"打开,但只有父div应该打开,我该怎么做?
// global variables
var nav = $('.group'),
navHeight = nav.height()+15,
items = $('.group .item .trigger'),
itemsSub = items.next('.toggle_container'),
itemsSubOpen = false,
speed = 400;
// global functions
var navOpen = function (thisSubmenu) {
itemsSubOpen = true;
// get height
thisSubmenu.css('height', 'auto');
var thisHeight = thisSubmenu.height();
thisSubmenu
.css('height', '0')
.animate({height: thisHeight }, speed)
.addClass('open');
nav.animate({height: thisHeight + navHeight }, speed);
};
var navClose = function () {
itemsSubOpen = false;
items.next('.open')
.animate({height: 0}, speed)
.removeClass('open');
nav.animate({height: navHeight-15 }, speed);
};
// prepare css
itemsSub.css('display', 'block');
itemsSub.css('height', '0');
// click event
items.click(function(event) {
// set local variables
var thisItem = $(this),
thisSubmenu = thisItem.next('.toggle_container');
// conditional click event handling
if ( itemsSubOpen ) {
if ( thisSubmenu.hasClass('open') ) {
// only close
navClose();
} else {
// close old, than open new
navClose();
setTimeout(function() {
navOpen(thisSubmenu);
}, speed);
}
} else {
// only open
navOpen(thisSubmenu);
}
// prevent default
event.preventDefault();
});
我这里有一个小提琴作为例子: http://jsfiddle.net/kfcgg/15/ 也许其中一个人可以帮我解决这个问题。
由于
答案 0 :(得分:1)
这样做的正确方法是你只提到父div!
$(this).closest('.group')
将引用父母'组'仅限课程。我希望这很有用!