0

我制作了一个下拉菜单,它使用选项卡来显示每个菜单项的特定内容。

这是我用于标签功能的 jQuery 教程的链接: http ://www.jacklmoore.com/notes/jquery-tabs

我已经修改了代码以满足我的需要,但是我无法找到一种方法来更改也应用了“活动”类的元素。出于 CSS 样式的原因,我希望将“活动”类应用于当前正在添加的父<li>类。<a>

我尝试了各种解决方案,包括该.parent();方法无济于事。这里的任何建议将不胜感激。

这是处理添加类的代码的快速片段:

// Keep track of which tab is active and it's associated content
var $active, $content, $links = $(this).find('li.activate-dropdown a');

// If the location.hash matches one of the links, use that as the active tab.
$active = $($links.filter('[rel="'+location.hash+'"]')[0]);
$active.addClass('active');
$content = $($active.attr('rel'));

编辑: 示例实时代码 - http://jsfiddle.net/jgfg7/3/

4

3 回答 3

0

如果您的代码适用于该<a>元素,则这应该适用于父级:

// Keep track of which tab is active and it's associated content
var $active, $content, $links = $(this).find('li.activate-dropdown a');

// If the location.hash matches one of the links, use that as the active tab.
$active = $($links.filter('[rel="'+location.hash+'"]')[0]);
$active.parent().addClass('active');
$content = $($active.attr('rel'));
于 2012-10-08T02:27:28.307 回答
0

你有没有尝试过

$active.parents("li").addClass('active');
于 2012-10-08T02:28:00.830 回答
0

在将.closest('li')类删除或添加到$active.

查看更新的小提琴:http: //jsfiddle.net/jgfg7/4/

于 2012-10-08T03:53:57.630 回答