質問

したがって、リストであり、サブリストとサブリストがあるナビゲーションがあります。

基本的に、ナビゲーションはデフォルトで折りたたまれていますが、ユーザーがサブリスト内のページをクリックした場合は、親リストを表示したいと考えています。また、サブリストのサブリストの場合は、両方の親リストを表示する必要があります。私はそれを設定しましたが、リストを展開するために 5 つの .parent().parent() を上向きにトラバースするのは好きではありません。もっと効率的な方法はありますか?

HTML:

<div id="lesson-sidebar">
        <ul>
            <li><a href="index.html">Welcome to Beat Basics and Beyond</a></li>
            <li><a href="table-of-contents.html">What's in this course?</a></li>
            <li><a href="defining-your-beat.html" class="active">Defining Your Beat</a>
                <ul>
                    <li><a href="boundaries-of-your-beat.html">Boundaries of Your Beat</a></li>
                    <li><a href="the-beat-description.html">The Beat Description</a></li>
                    <li><a href="build-your-own-beat-description.html"><span class="ital">Activity:</span> Build Your Own Beat Description</a></li>
                </ul>
            </li>
            <li><a href="getting-started.html">Getting Started</a>
                <ul>
                    <li><a href="debrief-your-predecessor.html">Debrief Your Predecessor</a></li>
                    <li><a href="predecessor-top-five-tips.html"><span class="ital">Activity:</span> List The Top Five Tips From Your Predecessor</a></li>
                    <li><a href="covering-your-beat-with-the-internet.html">Covering Your Beat With The Internet</a></li>
                    <li><a href="get-in-the-car-and-go.html">Get in the Car and Go</a></li>
                    <li><a href="mapping-your-beat.html">Mapping Your Beat</a></li>
                    <li><a href="read-the-clips.html">Read the Clips</a></li>
                    <li><a href="activity-dissect-this-clip.html"><span class="ital">Activity:</span> Dissect This Clip</a></li>
                    <li><a href="writing-your-first-article.html">Writing Your First Article</a></li>
                    <li><a href="starting-cold-on-the-beat.html">Starting Cold on the Beat</a></li>
                </ul>           
            </li>
            <li><a href="working-with-sources.html">Working With Sources</a>
                <ul>
                    <li><a href="finding-sources.html">Finding Sources</a></li>
                    <li><a href="diversify-your-sources.html">Diversify Your Sources</a></li>
                    <li><a href="prospecting-for-stories-and-sources.html">Prospecting for Stories and Sources</a></li>
                    <li><a href="building-relationships.html">Building Relationships</a></li>
                    <li><a href="going-off-the-record.html">Going Off the Record</a></li>
                </ul>
            </li>
            <li><a href="developing-resources.html">Developing Resources to Help You on the Beat</a>
                <ul>
                    <li><a href="develop-a-calendar-of-events.html">Develop a Calendar of Events</a></li>
                    <li><a href="build-your-library.html">Build Your Library</a></li>
                    <li><a href="learn-the-open-record-laws.html">Learn the Open Record Laws</a></li>
                </ul>
            </li>
            <li><a href="extra-resources.html">Extra Resources</a>
                <ul>
                    <li><a href="beat-breakdown-tool.html">Beat Breakdown Tool</a></li>
                    <li><a href="links-library.html">Links Library</a>
                        <ul>
                            <li><a href="general-resources-for-any-beat.html">General Resources for Any Beat</a></li>
                            <li><a href="courts-and-criminal-justice-links.html">Courts and Criminal Justice Links</a></li>
                            <li><a href="education-resources.html">Education Resources</a></li>
                            <li><a href="local-government-links.html">Local Government Links</a></li>
                            <li><a href="neighborhood-or-suburban-links.html">Neighborhood or Suburban Links</a></li>
                            <li><a href="police-and-public-safety-links.html">Police and Public Safety Links</a></li>
                            <li><a href="reporter-organizations.html">Reporter Organizations</a></li>
                        </ul>
                    </li>
                    <li><a href="additional-reading.html">Additional Reading</a></li>
                </ul>
            </li>
            <li><a href="final-thoughts.html">Final Thoughts</a></li>
        </ul>

jQuery:

function toggleSubmenu() {

    if ($(this).hasClass('submenu-hidden')) {

        $(this).parent().children('ul').slideToggle();
        $(this).removeClass().addClass('submenu-visible');

    } else if ($(this).hasClass('submenu-visible')) {

        $(this).parent().children('ul').slideToggle();
        $(this).removeClass().addClass('submenu-hidden');
    }
}

$('#lesson-sidebar ul ul').hide();
$('#lesson-sidebar ul ul ul').hide();
$('#lesson-sidebar ul:first-child').attr('id', 'rootlist');
$('#lesson-sidebar ul li:has("ul")').prepend('<span class="submenu-hidden"></span>').css('list-style','none');

$('#lesson-sidebar ul li a').each(
    function() {
        if ($(this).hasClass('active')) {
            // if it is a UL
            var length = $(this).parent().find("ul").length;
            alert(length);
            if (length == 0) {
                if ($(this).parent().parent().parent().children('span').hasClass('submenu-hidden')) {
                        $(this).parent().parent().parent().children('ul').show();
                        $(this).parent().parent().parent().children('span').removeClass('submenu-hidden').addClass('submenu-visible');
                }
                if ($(this).parent().parent().parent().parent().parent().children('span').hasClass('submenu-hidden')) {
                        $(this).parent().parent().parent().parent().parent().children('ul').show();
                        $(this).parent().parent().parent().parent().parent().children('span').removeClass('submenu-hidden').addClass('submenu-visible');
                } 
            }
            if (length == 1) {
                $(this).parent().find('ul').slideToggle();
                $(this).parent().children('span').removeClass('submenu-hidden').addClass('submenu-visible');
            }               
        }
    }
);

$('ul#rootlist > li span, ul#rootlist li ul li > span').bind('click', toggleSubmenu);

あらゆるご支援をいただければ幸いです。

役に立ちましたか?

解決

あなたが何をしようとしているのか理解できれば...次のようなことができます:

// For my benefit, hide all lists except the root items
$('ul, li', $('#lesson-sidebar ul li')).hide();

// Show active parents and their siblings
$('li a.active').parents('ul, li').each(function() {
    $(this).siblings().andSelf().show();
});

// Show the active item and its siblings
$('li a.active').siblings().andSelf().show();

両親() そして 兄弟() どちらのメソッドもこの種のことに最適です。

編集:以前は親兄弟が表示されないバグがありました。この新しいバージョンをお試しください。

編集2:リスト項目ではなくアンカーの class="active" で動作するようになりました。

他のヒント

それは$(this).closest("ul")が見つかるまで

ulは両親を横断します。

http://docs.jquery.com/Traversing/closest#exprする

  

...

...要素自体をテストし、DOMツリーにその祖先を通って上方に横断することにより、セレクタに一致する最初の要素を取得します

ランスMcNearyの非常に有用な答えを簡単にするために、トリックを使用することです。

.parents([selector])
  

、DOM要素の集合を表すjQueryオブジェクトを考えます   .parents()メソッドは、私たちはこれらの祖先を検索することができます   DOMツリー内の要素とから新しいjQueryオブジェクトを構築   アップの直接の親から注文した要素にマッチします。要素   外側のものに最も近い親から順に返されます。

別のユーザーが示唆されます:

.closest([selector])

.parentsと同様に()、これは、それが探している要素を見つけたら、それが停止したとして、より良い選択かもしれません。それはこの場合には、より効率的であるように思えます。 詳細については http://api.jquery.com/closest/ のを参照してください。 これは、人々が(.closest)と.parents()とどのように強力かつ柔軟なjQueryのは可能との違いを理解するのに役立ちます願っています。

$(this).parents().get()[4]はあなたの五分の一を与えるだろう。

このコード行で試してみてください。

$(this).parent().parent().fadeOut();
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top