jquery选择父母解开和排除孩子的麻烦

时间:2012-03-16 19:22:16

标签: jquery

我有一段时间与家人和孩子一起打瞌睡。以下是示例代码:

<li class="toplevel menu-item menu-item-type-post_type menu-item-object-page menu-item-95" id="menu-item-95"><a href="http://mysite.com/something">some word</a>
    <ul class="sub-menu">
        <li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-102" id="menu-item-102"><a href="http://mysite.com/something">wome word</a></li>
        <li class="nolink menu-item menu-item-type-custom menu-item-object-custom menu-item-103" id="menu-item-103"><a href="http://mysite.com/our-coalitions">projects</a>
            <ul class="sub-menu">
                <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-110" id="menu-item-110"><a href="http://mysite.com/proje1">project</a></li>
                <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-111" id="menu-item-111"><a href="http://mysite.com/proj2">project2</a></li>
                <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-112" id="menu-item-112"><a href="http://mysite.com/proj3">project 3</a></li>
            </ul>
        </li>
        <li class="nolink menu-item menu-item-type-custom menu-item-object-custom current-menu-item current_page_item menu-item-home menu-item-104" id="menu-item-104"><a href="http://mysite.com">another link</a></li>
    </ul>
</li>

我要做的是删除nolink类的超链接,同时保持<li>完整,所以我有一些有效的东西:

<li class="toplevel menu-item menu-item-type-post_type menu-item-object-page menu-item-95" id="menu-item-95"><a href="http://mysite.com/something">some word</a>
    <ul class="sub-menu">
        <li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-102" id="menu-item-102"><a href="http://mysite.com/something">wome word</a></li>
        <li class="nolink menu-item menu-item-type-custom menu-item-object-custom menu-item-103" id="menu-item-103">projects
            <ul class="sub-menu">
                <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-110" id="menu-item-110"><a href="http://mysite.com/proje1">project</a></li>
                <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-111" id="menu-item-111"><a href="http://mysite.com/proj2">project2</a></li>
                <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-112" id="menu-item-112"><a href="http://mysite.com/proj3">project 3</a></li>
            </ul>
        </li>
        <li class="nolink menu-item menu-item-type-custom menu-item-object-custom current-menu-item current_page_item menu-item-home menu-item-104" id="menu-item-104">another </li>
    </ul>
</li>

对如何做到这一点非常困惑!当我使用$('.nolink a').unwrap()时,它会解开我想要的和孩子们。

1 个答案:

答案 0 :(得分:2)

尝试:$('.nolink>a').contents().unwrap();​

证明:http://jsfiddle.net/iambriansreed/EbHvP/

>确保仅在a下直接抓取.nolink。 (咝咝声)