简体   繁体   English

页面加载后如何使用jQuery删除li元素?

[英]How to remove the li element using jQuery after page load?

I have indeed searched and Googled this and have seen the remove function but all the examples I have seen are doing it in a different context. 我确实已经搜索并用Google搜索了此功能,并看到了remove函数,但是我所看到的所有示例都在不同的上下文中执行此操作。 I just want the li removed that has the logout.html link. 我只希望删除具有logout.html链接的li。 I know you cannot assign id's to li elements so how to delete a particular element on the fly? 我知道您不能将id分配给li元素,因此如何动态删除特定元素?

Thanks 谢谢

<ul data-role="listview">
                        <li>
                            <a href="user_settings.html">
                                <img src="images/info/info-settings.png" width="30" height="30" class="ui-li-icon"/>
                                <h2>User Settings</h2>
                            </a>
                        </li>
                        <li>
                            **<a href="logout.html">
                                <img src="images/info/info-logout.png" width="30" height="30" class="ui-li-icon"/>
                                <h2>Logout</h2>
                            </a>**
                        </li>
                        <li>
                            <a href="http://someurl.com/store?app=ok" rel="external">
                                <img src="images/info/info-also-from.png" width="30" height="30" class="ui-li-icon"/>
                                <h2>Also from Us</h2>
                            </a>
                        </li>
                        <li>
                            <a href="about_us.html">
                                <img src="images/info/info-aboutUs.png" width="30" height="30" class="ui-li-icon"/>
                                <h2>About Us</h2>
                            </a>
                        </li>
                        <li>
                            <a href="about_us.html">
                                <img src="images/info/infsomeimage.png" width="30" height="30" class="ui-li-icon"/>
                                <h2>About Us</h2>
                            </a>
                        </li>
                        <li>
                            <a href="about_legal_info.html">
                                <img src="images/info/info-legalInfo.png" width="30" height="30" class="ui-li-icon"/>
                                <h2>Legal Info</h2>
                            </a>
                        </li>
                        <li>
                            <a href="about_international_dates.html">
                                <img src="images/info/info-international.png" width="30" height="30" class="ui-li-icon"/>
                                <h2>About International Dates</h2>
                            </a>
                        </li>
                    </ul>

There is nothing stopping you adding an id to an li element. 没有什么可以阻止您向li元素添加id的。 It is completely valid. 这是完全有效的。 However, without one you could use the has method: 但是,没有一个,您可以使用has方法:

$("li").has("a[href='logout.html']").remove();

This selects all li elements then reduces that set to only those which contain an element matching the selector. 这将选择所有li元素,然后将其减少为仅包含与选择器匹配的元素的那些元素。 It uses an attribute equals selector to match the href to your logout link. 它使用属性等于选择器将href匹配到您的注销链接。

There are numerous other ways you could do this. 还有许多其他方法可以执行此操作。 Another example would be the :contains selector, which matches text (and not a selector) within the element or any of its descendants: 另一个示例是:contains选择器,它与元素或其任何后代中的文本(而不是选择器)匹配:

$("li:contains('Logout')").remove();

Obviously you will have to be careful as if there are multiple elements matching the selectors in either case, they will all be removed. 显然,您将必须小心,因为在任何一种情况下都存在多个与选择器匹配的元素,它们都将被删除。 If that's the case, you will have to make the selectors more specific in some way. 在这种情况下,您将必须以某种方式使选择器更加具体。

You can add an id to li elements. 您可以为li元素添加一个id

To remove it with jQuery without doing so: 要使用jQuery删除它而不这样做:

$('li a[href="logout.html"]').closest('li').remove();

Example fiddle . 小提琴的例子

$(document).ready(function() {

  $('li a[href="user_settings.html"]').parents('li').first().remove();

});

First of all you can assign ids to li elements. 首先,您可以为li元素分配ID。 You can have <li id="some_id" ></li> 您可以具有<li id="some_id" ></li>

Secondly you can hide your li element using the following code (without id) - or view a working example here 其次,您可以使用以下代码(没有ID)隐藏li元素-或在此处查看工作示例

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

This should be put in the document's onload callback, so it gets executed after loading the page: 这应该放在文档的onload回调中,以便在加载页面后执行:

$(function (){
    $('a[href="logout.html"]').parent().css('display', 'none');
});

$("ul li:nth-child(2)").remove(); this will work if your logout.html li should be 2nd element always or you can try $('a[href="logout.html"]').parent().remove(); 如果您的logout.html li应该始终是第二个元素,或者您可以尝试$('a[href="logout.html"]').parent().remove(); ;,这将起作用$('a[href="logout.html"]').parent().remove();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM