0
我不知道jquery好,所以請不要評價我。Jquery垂直手風琴展開如果活躍
我有手風琴效果的垂直菜單,我想:如果我點擊子元素,比手風琴菜單不會崩潰,但保持展開直到我選擇另一個元素。現在,每次點擊後菜單都會崩潰。
我的手風琴菜單的樣子:
<ul class="menu menu-sidebar">
<li class="level1 item139 parent active">
<span class="separator level1 parent active">
<span>Menu item name</span>
</span>
<div style="display: block; height: 146px;">
<ul class="nav-child unstyled small level2">
<li class="level2 item140">
<a href="custom-link">
<span>Level 2 item name</span>
</a>
</li>
<li class="level2 item141">
<a href="custom-link2">
<span>Level 2 item name2</span>
</a>
</li>
</ul>
</div>
</li>
</ul>
jQuery腳本:
(function (e) {
var a = function() {};
e.extend(a.prototype, {
name: "accordionMenu",
options: {
mode: "default",
display: null,
collapseall: !1,
toggler: "span.level1.parent",
content: "ul.level2",
onaction: function() {}
},
initialize: function (a, b) {
var b = e.extend({}, this.options, b),
f = a.find(b.toggler);
f.each(function (a) {
var c = e(this),
d = c.next(b.content).wrap("<div>").parent();
d.data("height", d.height());
c.hasClass("active") || a == b.display ? d.show() : d.hide().css("height", 0);
c.bind("click", function() {
g(a)
})
});
var g = function (a) {
var c =
e(f.get(a)),
d = e([]);
c.hasClass("active") && (d = c, c = e([]));
b.collapseall && (d = f.filter(".active"));
switch (b.mode) {
case "slide":
c.next().stop().show().animate({
height: c.next().data("height")
}, 400);
d.next().stop().animate({
height: 0
}, 400, function() {
d.next().hide()
});
setTimeout(function() {
b.onaction.apply(this, [c, d])
}, 401);
break;
default:
c.next().show().css("height", c.next().data("height")), d.next().hide().css("height", 0), b.onaction.apply(this, [c, d])
}
c.addClass("active").parent().addClass("active");
d.removeClass("active").parent().removeClass("active")
}
}
});
e.fn[a.prototype.name] = function() {
var h = arguments,
b = h[0] ? h[0] : null;
return this.each(function() {
var f = e(this);
if (a.prototype[b] && f.data(a.prototype.name) && "initialize" != b) f.data(a.prototype.name)[b].apply(f.data(a.prototype.name), Array.prototype.slice.call(h, 1));
else if (!b || e.isPlainObject(b)) {
var g = new a;
a.prototype.initialize && g.initialize.apply(g, e.merge([f], h));
f.data(a.prototype.name, g)
} else e.error("Method " + b + " does not exist on jQuery." + a.name)
})
}
})(jQuery);
Mybe你可以幫助修改我現有的代碼嗎? – Newcomer
如何檢測子元素是否具有活動類?如果子元素具有活動類,則父元素將展開,否則 - 摺疊。 – Newcomer
@Newcomer http://api.jquery.com/hasClass/ – UDB