简体   繁体   English

通过 Vanilla JS 使用 InnerText 查找元素并删除

[英]Find an Element and Delete using the InnerText via Vanilla JS

I'm trying to find and remove an element using the InnerText with vanilla JS.我正在尝试使用带有 vanilla JS 的 InnerText 查找和删除一个元素。 Can not use jQuery, or edit the existing website code, even though it would be much simpler.不能使用 jQuery,或者编辑现有的网站代码,即使它会简单得多。 The 'new' injected code has to handle the work. “新”注入的代码必须处理这项工作。

Because the div classes are not unique and the order is not always consistent, I need a way to hook into the element and delete it with JS.因为 div 类不是唯一的,顺序也不总是一致的,所以我需要一种方法来钩入元素并用 JS 将其删除。 The code to remove the elements will be deployed in the header through an AB testing tool, ie Optimizely.删除元素的代码将通过AB测试工具Optimizely部署在header中。 I'm able to delete the elements with class name 'rate-card', but I only want to remove the one that says 'Clean Care' in the title.我可以删除名称为“rate-card”的 class 的元素,但我只想删除标题中显示“Clean Care”的元素。

Here's the website code I'm trying to identify and delete.这是我试图识别和删除的网站代码。

 <div class="collapse d-md-block" id="ItemRates-1"> <div class="rate-card"> <div class="rate-card-title"> <strong>Best Rate</strong> <button type="button" class="btn-plain tooltip" data-toggle="modal" data-target="#PK1" data-original-title="" title=""> </button> </div> </div> <div class="rate-card"> <div class="rate-card-title"> <strong>Clean Care</strong> <button type="button" class="btn-plain tooltip" data-toggle="modal" data-target="#PK2" data-original-title="" title=""> </button> </div> </div> </div>

Here's the Code I have so far.这是我到目前为止的代码。 Able to delete the element with class name 'rate-card', but I only want to delete the element with the title 'Clean Care'.能够删除 class 名称为“rate-card”的元素,但我只想删除标题为“Clean Care”的元素。 Elements are not always in the same order, so using the child order statement to hook the element is not possible.元素的顺序并不总是相同的,因此使用子顺序语句来挂钩元素是不可能的。

 <script> const elem= document.getElementsByClassName("rate-card") while (elem.length > 0 ) elem[0].remove(); </script>

If you put the title in a data-attribute (its value displayed as a :before pseudo), you can use a simple query and forEach to remove the element(s).如果将标题放在数据属性中(其值显示为:before伪),则可以使用简单的查询和forEach删除元素。 Something like:就像是:

 setTimeout(() => document.querySelectorAll(`[data-title='Clean Care']`).forEach(elem => elem.closest(`.rate-card`).remove()), 1500 );
 .rate-card-title:before { content: attr(data-title); font-weight: bold; }.btn-plain:before { content: 'Select...'; }
 <div class="collapse d-md-block" id="ItemRates-1"> <div class="rate-card"> <div class="rate-card-title" data-title="Best Rate"> <button type="button" class="btn-plain tooltip" data-toggle="modal" data-target="#PK1" data-original-title="" title=""> </button> </div> </div> <div class="rate-card"> <div class="rate-card-title" data-title="Clean Care"> (I will be deleted soon...) <button type="button" class="btn-plain tooltip" data-toggle="modal" data-target="#PK2" data-original-title="" title=""> </button> </div> </div> </div>

You will have to look for the text in the string element and remove it.您必须在字符串元素中查找文本并将其删除。

 document.querySelectorAll(".rate-card").forEach(card => { const text = card.querySelector("strong").innerText; if(text === "Clean Care") { card.remove(); } });
 <div class="collapse d-md-block" id="ItemRates-1"> <div class="rate-card"> <div class="rate-card-title"> <strong>Best Rate</strong> <button type="button" class="btn-plain tooltip" data-toggle="modal" data-target="#PK1" data-original-title="" title=""> </button> </div> </div> <div class="rate-card"> <div class="rate-card-title"> <strong>Clean Care</strong> <button type="button" class="btn-plain tooltip" data-toggle="modal" data-target="#PK2" data-original-title="" title=""> </button> </div> </div> </div>

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

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