[英]Remove text in between anchors tags and also remove second <a> with jquery?
I have SharePoint page which is generated. 我有生成的SharePoint页面。 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. 需要删除:文本为
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, "http://spfoundation/d"); 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? 我们如何用jQuery做到这一点? I tried with
$("td[class=ms-list-addnew*] a:nth-child(2)").text("");
我试过
$("td[class=ms-list-addnew*] a:nth-child(2)").text("");
. 。 But did not worked.
但是没有用。
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 您需要使用
contents
来过滤掉textNodes
http://jsfiddle.net/evilbuck/hh1598gk/ 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. 对于jQuery,您需要使用
contents()
来访问DOM元素中的原始文本节点。 You can then check the nodeType to match text nodes (3 = text node: http://www.w3schools.com/dom/dom_nodetype.asp ) 然后,您可以检查nodeType以匹配文本节点(3 =文本节点: http : //www.w3schools.com/dom/dom_nodetype.asp )
The index passed to filter
lets you identify a specific element: 传递给
filter
的索引使您可以标识特定元素:
JSFiddle: http://jsfiddle.net/TrueBlueAussie/53w8o35f/1/ 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. 请进行相应调整以删除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
. 另一种选择是简单地匹配要保留的内容,并在
td
上使用html(itemsIWantToKeep)
。 You can supply a function to html()
so that the contents of each is applied to itself only. 您可以为
html()
提供一个函数,以便每个函数的内容仅适用于自身。
eg http://jsfiddle.net/TrueBlueAussie/53w8o35f/2/ 例如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("")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.