当a-tag包含“_sk”时,jquery隐藏父标记

时间:2013-04-02 03:28:07

标签: jquery

我有以下html:

    <div id="secondary">
        <ul class="simple-pages-navigation">
            <li class="nav-das-projekt current"><a href="/projekt">Das Projekt</a>
                <ul class="subnav-1">
                    <li class="nav-ber-kaschau"><a href="/ber-kaschau">Über Kaschau</a></li>
                    <li class="nav-projektpartner"><a href="/projektpartner">Projektpartner</a></li>
                </ul>
            </li>
            <li class="nav-projektu"><a href="/projekt_sk">Projekt</a>
                <ul class="subnav-1">
                    <li class="nav-o-koiciach"><a href="/ber-kaschau_sk">O Košiciach</a></li>
                    <li class="nav-o-koiciach"><a href="/partneri_sk">O Košiciach</a></li>
                </ul>
            </li>
            <li class="nav-project"><a href="/projekt_en">Project</a>
                <ul class="subnav-1">
                    <li class="nav-o-koiciach"><a href="/ber-kaschau_en">About Kaschau</a></li>
                    <li class="nav-o-koiciach"><a href="/partner_en">Project Partners</a></li>
                </ul>
            </li>
        </ul>
    </div>

I want to add css `display:none;` via jQuery to all parent-parent tags whose daughter-tags have a link ending with `"_sk"` or `"_en"` in it.
the result should look like this:

    <div id="secondary">
        <ul class="simple-pages-navigation">
            <li class="nav-das-projekt current"><a href="/projekt">Das Projekt</a>
                <ul class="subnav-1">
                    <li class="nav-ber-kaschau"><a href="/ber-kaschau">Über Kaschau</a></li>
                    <li class="nav-projektpartner"><a href="/projektpartner">Projektpartner</a></li>
                </ul>
            </li>
            <li class="nav-projektu" style="display: none"><a href="/projekt_sk">Projekt</a>
                <ul class="subnav-1">
                    <li class="nav-o-koiciach"><a href="/ber-kaschau_sk">O Košiciach</a></li>
                    <li class="nav-o-koiciach"><a href="/partneri_sk">O Košiciach</a></li>
                </ul>
            </li>
            <li class="nav-project" style="display: none"><a href="/projekt_en">Project</a>
                <ul class="subnav-1">
                    <li class="nav-o-koiciach"><a href="/ber-kaschau_en">About Kaschau</a></li>
                    <li class="nav-o-koiciach"><a href="/partner_en">Project Partners</a></li>
                </ul>
            </li>
        </ul>
    </div>

===编辑:===

我添加了更多HTML,现在是答案

$('a[href$="_sk"], a[href$="_hu"]').parent().hide();

在这个html代码中不起作用,我无法找出原因,因为它仍适用于以“_sk”结尾的a-tag-links,但不适用于带有“_hu”的链接:

<div id="secondary">
    <ul class="simple-pages-navigation">
        <li class="nav-a-projekt"><a href="/projekt_hu">A Projekt</a>
            <ul class="subnav-1">
                <li class="nav-difmoe" style="display: list-item;"><a href="/difmoe_hu">DiFMOE ,</a></li>
                <li class="nav-kassrl" style="display: list-item;"><a href="/ber-kaschau_hu">Kassáról</a></li>
                <li class="nav-projektpartnerek" style="display: list-item;"><a href="/projektpartner_hu">Projektpartnerek</a></li>
            </ul>
        </li>
        <li class="nav-das-projekt current"><a href="/projekt_de">Das Projekt</a>
            <ul class="subnav-1">
                <li class="nav-ber-kaschau"><a href="/ber-kaschau_de">Über Kaschau</a></li>
                <li class="nav-projektpartner"><a href="/projektpartner_de">Projektpartner</a></li>
                <li class="nav-difmoe"><a href="/difmoe_de">DiFMOE</a></li>
            </ul>
        </li>
        <li class="nav-die-digitale-stadtbibliothek" style="display: none;"><a href="http://cassovia-digitalis.eu/">Die Digitale Stadtbibliothek</a></li>
        <li class="nav-die-digitale-stadtbibliothek---en" style="display: none;"><a href="/startseite_en">Die Digitale Stadtbibliothek - EN</a></li>
        <li class="nav-die-digitale-stadtbibliothek---hu" style="display: none;"><a href="/startseite_hu">Die Digitale Stadtbibliothek - HU</a></li>
        <li class="nav-die-digitale-stadtbibliothek---sk" style="display: none;"><a href="/startseite_sk">Die Digitale Stadtbibliothek - SK</a></li>
        <li class="nav-projekt" style="display: none;"><a href="/projekt_sk">Projekt</a>
            <ul class="subnav-1">
                <li class="nav-o-koiciach" style="display: none;"><a href="/ber-kaschau_sk">O Košiciach</a></li>
                <li class="nav-partneri-projektu" style="display: none;"><a href="/projektpartner_sk">Partneri projektu</a></li>
                <li class="nav-difmoe" style="display: none;"><a href="/difmoe_sk">DiFMOE .</a></li>
            </ul>
        </li>
        <li class="nav-bibliothek"><a href="/bibliothek_de">Bibliothek</a>
            <ul class="subnav-1">
                <li class="nav-periodika"><a href="/periodika_de">Periodika</a>
                    <ul class="subnav-2">
                        <li class="nav-zeitungen"><a href="/zeitungen_de">Zeitungen</a>
                            <ul class="subnav-3" style="display: none;">
                                <li class="nav-felsmagyarorszag"><a href="/felsoemagyarorszag">Felsömagyarorszag</a></li>
                                <li class="nav-kaschau-eperieser-kundschaftsblatt"><a href="/kundschaftsblatt_de">Kaschau-Eperieser Kundschaftsblatt</a></li>
                                <li class="nav-kassai-szemle"><a href="/kassai-szemle_de">Kassai Szemle</a></li>
                            </ul>
                        </li>
                        <li class="nav-zeitschriften"><a href="/zeitschriften_de">Zeitschriften</a>
                            <ul class="subnav-3" style="display: none;">
                                <li class="nav-der-bote-von-und-fr-ungern"><a href="/bote-ungarn_de">Der Bote von und für Ungern</a></li>
                                <li class="nav-pannonia"><a href="/pannonia_de">Pannonia</a></li>
                            </ul>
                        </li>
                        <li class="nav-volkskalender"><a href="/volkskalender_de">Volkskalender</a></li>
                    </ul>
                </li>
                <li class="nav-bcher"><a href="/monographien_de">Bücher</a>
                    <ul class="subnav-2">
                        <li class="nav-gesamtliste"><a href="/monographien_sortdate">Gesamtliste</a></li>
                    </ul>
                </li>
                <li class="nav-bildersammlung"><a href="/bildersammlung_de">Bildersammlung</a>
                    <ul class="subnav-2">
                        <li class="nav-postkarten"><a href="/postkarten_de">Postkarten</a></li>
                        <li class="nav-karten"><a href="/karten_de">Karten</a></li>
                    </ul>
                </li>
                <li class="nav-sondersammlungen"><a href="/sondersammlung">Sondersammlungen</a>
                    <ul class="subnav-2">
                        <li class="nav-alltag"><a href="/alltag_de">Alltag</a></li>
                        <li class="nav-eugen-deil"><a href="/eugen-deil_de">Eugen Deil</a></li>
                        <li class="nav-theaterplakate"><a href="/theaterplakate_de">Theaterplakate</a></li>
                        <li class="nav-handwerk"><a href="/wanderbuecher_de">Handwerk</a></li>
                        <li class="nav-adressbcher"><a href="/adressbuecher_de">Adressbücher</a></li>
                    </ul>
                </li>
            </ul>
        </li>
        <li class="nav-kninica" style="display: none;"><a href="/bibliothek_sk">Knižnica</a>
            <ul class="subnav-1">
                <li class="nav-periodik" style="display: none;"><a href="/periodika_sk">Periodiká.</a>
                    <ul class="subnav-2">
                        <li class="nav-zeitungen_sk" style="display: none;"><a href="/zeitungen_sk">Zeitungen_sk</a></li>
                        <li class="nav-zeitschriften_sk" style="display: none;"><a href="/zeitschriften_sk">Zeitschriften_sk</a></li>
                        <li class="nav-volkskalender_sk" style="display: none;"><a href="/volkskalender_sk">Volkskalender_sk</a></li>
                    </ul>
                </li>
                <li class="nav-knihy" style="display: none;"><a href="/monographien_sk">Knihy</a>
                    <ul class="subnav-2">
                        <li class="nav-gesamtliste_sk" style="display: none;"><a href="/monographien_sortdate_sk">Gesamtliste_sk</a></li>
                    </ul>
                </li>
                <li class="nav-obrazov-materil" style="display: none;"><a href="/bildersammlung_sk">Obrazový materiál</a>
                    <ul class="subnav-2">
                        <li class="nav-postkarten_sk" style="display: none;"><a href="/postkarten_sk">Postkarten_sk</a></li>
                        <li class="nav-karten_sk" style="display: none;"><a href="/karten_sk">Karten_sk</a></li>
                    </ul>
                </li>
                <li class="nav-pecilne-zbierky" style="display: none;"><a href="/sondersammlung_sk">Špeciálne zbierky</a>
                    <ul class="subnav-2">
                        <li class="nav-alltag_sk" style="display: none;"><a href="/alltag_sk">Alltag_sk</a></li>
                        <li class="nav-theaterplakate_sk" style="display: none;"><a href="/theaterplakate_sk">Theaterplakate_sk</a></li>
                        <li class="nav-eugen-deil" style="display: none;"><a href="/eugen-deil_sk">Eugen Deil.</a></li>
                        <li class="nav-handwerk_sk" style="display: none;"><a href="/wanderbuecher_sk">Handwerk_sk</a></li>
                        <li class="nav-adressbcher_sk" style="display: none;"><a href="/adressbuecher_sk">Adressbücher_sk</a></li>
                    </ul>
                </li>
            </ul>
        </li>
        <li class="nav-knyvtr" style="display: list-item;"><a href="/bibliothek_hu">Könyvtár</a>
            <ul class="subnav-1">
                <li class="nav-idszaki-kiadvnyok" style="display: list-item;"><a href="/periodika_hu">Időszaki kiadványok</a>
                    <ul class="subnav-2">
                        <li class="nav-zeitungen_hu" style="display: list-item;"><a href="/zeitungen_hu">Zeitungen_hu</a></li>
                    </ul>
                </li>
            </ul>
        </li>
    </ul>
</div>

有谁知道为什么以及如何解决它?

2 个答案:

答案 0 :(得分:3)

您可以在jQuery选择器中使用$来获取链接结尾。

  

属性以选择器[name$="value"]结束

     

描述:选择具有指定属性的元素,其值以与给定字符串完全相同的结尾。比较区分大小写。

Official Document

示例

$('a[href$="_sk"]').css('display','none');

答案 1 :(得分:1)

尝试

$('a[href$="_sk"], a[href$="_en"]').parent().hide();

演示:Fiddle