1

我希望替换ul父 ( li.filetabs) 元素的子元素 ( ) 的类名。我所做的似乎很正确,但显然不是因为班级没有被替换。

用JSFIDDLE 更新 Jsfiddle 的注意事项:当您单击一次时,一切正常,再次单击相同的导航选项卡,您会看到下拉菜单消失并重新出现...

var aFileTabs = $('.filetabs');
var curTab = $(this);
// this seems to give correct output 
var prevCurTab = $('.cTabActive').parent(); //(i.e. *jQuery(li.filetabs)*  )
// the following line is the problem line; Ive tried replacing the children with
// $('ul',prevCurTab)... but that doesnt work either... I must be doing something
// very basic wrong
$(prevCurTab).children().removeClass('cTabActive').addClass('cPrevTabActive');
$('ul', curTab).addClass ('cTabActive');

编辑:抱歉,我忘记了 HTML,还有更多内容,所以只有前两个导航按钮 html 被显示以了解含义。

    <div id="filemenu">                     <!-- right tabs menu -->
        <ul id="fm_ul">
            <li class="filetabs">File
                <ul>
                    <li class="m_items"><a href="#"><span class="aHeading">New</span><span class="shortcutK">Ctrl-U</span></a></li>
                    <li class="m_items"><a href="#"><span class="aHeading">Open</span><span class="shortcutK">Ctrl-Z</span></a></li>
                    <li class="m_items"><a href="#"><span class="aHeading">Save</span><span class="shortcutK">Ctrl-T</span></a></li>
                    <li class="m_items"><a href="#"><span class="aHeading">Exit</span><span class="shortcutK">Ctrl-Q</span></a></li>
                </ul></li><li class="filetabs">Edit
<ul>
                    <li class="m_items"><a href="#"><span class="aHeading">Undo</span><span class="shortcutK">Ctrl-M</span></a></li>
                    <li class="m_items"><a href="#"><span class="aHeading">Redo</span><span class="shortcutK">Ctrl-S</span></a></li>
                    <li class="m_items"><a href="#"><span class="aHeading">Zoom In</span><span class="shortcutK">Ctrl-R</span></a></li>
                    <li class="m_items"><a href="#"><span class="aHeading">Zoom Out</span><span class="shortcutK">Ctrl-Q</span></a></li>

                </ul></li>
4

2 回答 2

0

我不确定我是否明白这一点,但请尝试:

$('.filetabs').on('click', function() {
    var aFileTabs = $('.filetabs'),
        curTab = $(this),
        prevCurTab = $('.cTabActive').parent();

    $('.cTabActive').removeClass('cTabActive').addClass('cPrevTabActive');
    $('ul', curTab).removeClass('cPrevTabActive').addClass ('cTabActive');
});
​

小提琴

于 2012-05-15T17:52:29.530 回答
0

在这个单独的帖子中回答。谢谢丹尼尔。

Jquery - 在一个简单的下拉菜单中操作类

于 2012-05-26T07:51:17.813 回答