簡體   English   中英

刪除錨標簽之間的文本,還刪除<a>與jQuery</a>第二<a>?</a>

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

我有生成的SharePoint頁面。 現在,我試圖從下面的文本中刪除文本和第二個錨標記。

需要刪除:文本為or (在錨標記之間),並刪除第二個錨或錨文本。

<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>

我們如何用jQuery做到這一點? 我試過$("td[class=ms-list-addnew*] a:nth-child(2)").text(""); 但是沒有用。

更新

要刪除

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>

正如評論中提到的。 您需要使用contents來過濾掉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();

對於jQuery,您需要使用contents()來訪問DOM元素中的原始文本節點。 然后,您可以檢查nodeType以匹配文本節點(3 =文本節點: http : //www.w3schools.com/dom/dom_nodetype.asp

傳遞給filter的索引使您可以標識特定元素:

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();

請進行相應調整以刪除HTML中的所有組件。 這只是我對您想要的剩下的最好的猜測。

更簡單的選擇:(保留所需的位)

另一種選擇是簡單地匹配要保留的內容,並在td上使用html(itemsIWantToKeep) 您可以為html()提供一個函數,以便每個函數的內容僅適用於自身。

例如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'))

嘗試這個:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM