简体   繁体   English

是否可以从元素内的 DOM 中删除 textNode?

[英]Is it possible to delete a textNode from the DOM within an element?

I am using a SharePoint blog and for some odd reason, the post to every blog reads,我正在使用 SharePoint 博客,出于某种奇怪的原因,每个博客的帖子都写着,

by [object Object] in HR Department按 [object Object] 在 HR 部门

I am trying to use DOM manipulation to remove this but it's to no avail.我正在尝试使用 DOM 操作来删除它,但无济于事。 The HTML looks something like the below. HTML 如下所示。 I want to remove by [object Object] in我想通过 [object Object]删除

<div class="ms-metadata ms-textSmall">
    <span> 
      by [object Object] in <a class="ms-link" id="blgcat" href="https://myPortal.net">IT Department</a>
    </span>
</div>

I tried the following but it didn't work我尝试了以下但没有用

var link = document.getElementById("blgcat").parentNode.textNode;
console.log(link)
var leafSibling = link.previousSibling
link.removeChild(leafSibling)

Any idea how to fix?知道如何解决吗? Here's the pen .这是

You were close.你很接近。 Start with just the known element...the link.从已知元素开始……链接。

Then get the link's previous sibling and remove that from link's parent node.然后获取链接的前一个兄弟节点并将其从链接的父节点中删除。

 var link = document.getElementById("blgcat"); link.parentNode.removeChild(link.previousSibling)
 <div class="ms-metadata ms-textSmall"> <span> by [object Object] in <a class="ms-link" id="blgcat" href="https://myPortal.net">IT Department</a> </span> </div>

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

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