简体   繁体   中英

Remove text in between anchors tags and also remove second <a> with jquery?

I have SharePoint page which is generated. Now I am trying to remove text and second anchor tags from the below text.

Need to remove: Text is or (which is in between anchor tags) and remove second anchor or anchor text.

<table id="Hero-WPQ2" dir="none" cellpadding="0" cellspacing="0" border="0">
   <tbody>
       <tr>
          <td class="ms-list-addnew ms-textXLarge ms-list-addnew-aligntop ms-soften">
               <a id="idHomePageNewItem" class="ms-heroCommandLink" href="http://spfoundation/d" data-viewctr="5" onclick="NewItem2(event, &quot;http://spfoundation/d&quot;); return false;" target="_self" title="Add a new item to this list or library.">
               <span class="ms-list-addnew-imgSpan20">
               <img id="idHomePageNewItem-img" src="/dept/it/" class="ms-list-addnew-img20">
               </span>
                <span>new item</span>
                </a>

                 or 
                <a class="ms-heroCommandLink" href="javascript:;" onclick="EnsureScriptParams('inplview', 'InitGridFromView', '{23AB37B1-38E9-460B-BE87-FCAF204DAD20}'); return false;" title="Edit this list using Quick Edit mode.">edit</a>
                 this list
             </td>
        </tr>
     </tbody>

How can we do that with jquery? I tried with $("td[class=ms-list-addnew*] a:nth-child(2)").text(""); . But did not worked.

updated

Want to remove

or 
   <a class="ms-heroCommandLink" href="javascript:;" onclick="EnsureScriptParams('inplview', 'InitGridFromView', '{23AB37B1-38E9-460B-BE87-FCAF204DAD20}'); return false;" title="Edit this list using Quick Edit mode.">edit</a>

as was mentioned in the comments. You'll need to use contents to filter out the textNodes

http://jsfiddle.net/evilbuck/hh1598gk/

$('.ms-list-addnew a:nth-of-type(2)').remove();
$('.ms-list-addnew').contents().filter(function() {
    return this.nodeType === 3;
}).eq(1).remove();

For jQuery you need to use contents() to access raw text nodes in a DOM element. You can then check the nodeType to match text nodes (3 = text node: http://www.w3schools.com/dom/dom_nodetype.asp )

The index passed to filter lets you identify a specific element:

JSFiddle: http://jsfiddle.net/TrueBlueAussie/53w8o35f/1/

$('td[class^="ms-list-addnew"]').contents().filter(function(i) {
        return this.nodeType === 3 && i == 2;     // Node.TEXT_NODE
   }).remove();
$('td[class^="ms-list-addnew"] a:last').remove();

please adjust accordingly to remove any components in the HTML. This was just my best guess of what you wanted left.

Simpler option: (keep the bits you want)

Another option is to simply match the things you want to keep and use html(itemsIWantToKeep) on the td . You can supply a function to html() so that the contents of each is applied to itself only.

eg http://jsfiddle.net/TrueBlueAussie/53w8o35f/2/

$('td[class^="ms-list-addnew"]').html(function(){
    // Keep the first child of each td
    return $(this).children().first();    
});

那这个呢

$('table td').html($('table td :first-child'))

Try this:

$($("td[class=ms-list-addnew*] a")[2]).prev().text("")
$($("td[class=ms-list-addnew*] a")[2]).text("")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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