开发者

How to delete DOM element based on attribute value

I have this HTML code from Sharepoint and I was wondering if it's possible to delete this specific DOM element based only on the "text" attribute?

<ie:menuitem menugroupid="200" text="new docs view" onmenuclick="window.location = '/some link/';" type="option" id="zz36_View1"></ie:menuitem>

Using Firebug I can simply right click the above line and click delete element. How do I do this using Javascript w/ Jquery?

Here's some context of where the above line came from:

<span style="display:none">
    <menu compactmode="true" id="zz34_ViewSelectorMenu" type="ServerMenu">
        <ie:menuitem menugroupid="100" text="All Documents" onmenuclick="window.location = '/some link/';" type="option" id="zz35_DefaultView"></ie:menuitem>
        &l开发者_高级运维t;ie:menuitem menugroupid="200" text="new docs view" onmenuclick="window.location = '/some link/';" type="option" id="zz36_View1"></ie:menuitem>
        <ie:menuitem menugroupid="300" text="Explorer View" onmenuclick="window.location = '/some link/';" type="option" id="zz37_View2"></ie:menuitem>
    </menu>
</span>

I tried something like:

<script language="Javascript" src="http://code.jquery.com/jquery-1.5.2.min.js"></script>

<script language="Javascript">
    $(document).ready(function() {
        $('ie:menuitem[text*="new docs view"]').remove();
    });
</script>

but it doesn't really work.


Try

$('ie\\:menuitem[text*="new docs view"]').remove();
//or
$('menuitem[text*="new docs view"]').remove();

Just a guess since I really don't know how well jQuery works with namespaces.


$('ie:menuitem[text*="new docs view"]')

ie:menuitem is not a standard HTML element. Therefore jQuery is likely interpreting :menuitem as a selector. That's likely the root of your problem.


$('ie\\:menuitem[text*="new docs view"]').remove() may work.


That's how you can do. Assuming your menu is the first children: (dirty way)

$("span").children(1).children().each(function(){
    if($(this).attr("text") == "new docs view"){
        $(this).remove();
        return;
    }
});

Jsfiddle: http://jsfiddle.net/naveed_ahmad/wdTwL/


You have to escape the : in your selector:

$("ie\\:menuitem[type='option'][text='All Documents']").remove();

Shown working here: http://jsfiddle.net/Codemonkey/tduVq/1/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜