简体   繁体   English

有没有更好的方法通过类名获取特定元素来修改元素?

[英]Is there a better way to get the specific element by their class name to modify the elements?

I am trying to select a specific element so that I can delete or change the content of the element.我试图选择一个特定的元素,以便我可以删除或更改元素的内容。

For example, if this is the code I have例如,如果这是我的代码

<div class="container">
  <div class="box">
    <div class = "a"> OPTION 12 </div>
<div class="container">
  <div class="box">
    <div class = "b"> OPTION 78 </div>
</div>
<div class="container">
  <div class="box">
    <div class = "c"> OPTION 56 </div>
</div>
<div class="container">
  <div class="box">
    <div class = "d"> OPTION 90 </div>
</div>

If the order of the box gets rendered randomly when loading up a page, and I want to select a div that has OPTION 56 and delete the whole node that contains that content, what is the best way to approach this WHEN they render in the webpages randomly?如果加载页面时框的顺序随机呈现,并且我想选择一个具有 OPTION 56 的 div 并删除包含该内容的整个节点,那么当它们在网页中呈现时,最好的方法是什么随机?

I know you can remove the childNodes with getElementsByClassName and their indices.我知道您可以使用 getElementsByClassName 及其索引删除 childNode。 Is there a specific way to access the node/element without using the getElementsByClassName method?是否有一种特定的方法可以在不使用 getElementsByClassName 方法的情况下访问节点/元素?

I've tried adding an id for all each of the div classes.我尝试为所有每个 div 类添加一个 id。

I want to select a parent div that contains the content OPTION 56 and delete the whole我想选择一个包含内容 OPTION 56 的父 div 并删除整个

<div class="container">
  <div class="box">
    <div class = "c"> OPTION 56 </div>
</div>

Parent node.父节点。

Thanks in advance.提前致谢。

You can always put the value of the div into a data attribute.您始终可以将 div 的值放入数据属性中。

<div class="container">
    <div class="box">
        <div class="a" data-option="OPTION 12">OPTION 12</div>
    </div>
    <div class="box">
        <div class="c" data-option="OPTION 56">OPTION 56</div>
    </div>
</div>

And your javascript (or jquery, as using below) would involve你的javascript(或jquery,如下使用)将涉及

$('[data-option="OPTION 56"]').parent('.box').remove();   

Assuming you do not know the class name beforehand and only know the innerText value you want to remove, you can do this to remove the element:假设您事先不知道类名并且只知道要删除的innerText值,您可以这样做来删除元素:

 const removeMe = "OPTION 56"; const e = document.querySelectorAll('.container .box div'); e.forEach(e => (e.innerText === removeMe && e.remove()));
 <div class="container"> <div class="box"> <div class="a"> OPTION 12 </div> <div class="container"> <div class="box"> <div class="b"> OPTION 78 </div> </div> <div class="container"> <div class="box"> <div class="c"> OPTION 56 </div> </div> <div class="container"> <div class="box"> <div class="d"> OPTION 90 </div> </div> </div> </div> </div> </div> </div>

  1. Use jQuery's :contains selector to fined the element with the desired text:使用 jQuery 的:contains 选择器使用所需文本对元素进行细化:
    $("div:contains('OPTION 56')")
    (you can make it more specific by adding its parents in the selector). (您可以通过在选择器中添加其父项来使其更具体)。
  2. Use jQuery's parent to go to its parents.使用 jQuery 的父级访问其父级。
  3. Use jQuery's remove to remove the "grandfather" node.使用 jQuery 的remove删除“祖父”节点。

Here is a sample code:这是一个示例代码:

 $("div.container > div.box > div:contains('OPTION 56')").parent().parent().remove();
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="container"> <div class="box"> <div class = "a"> OPTION 12 </div> </div> </div> <div class="container"> <div class="box"> <div class = "b"> OPTION 78 </div> </div> </div> <div class="container"> <div class="box"> <div class = "c"> OPTION 56 </div> </div> </div> <div class="container"> <div class="box"> <div class = "d"> OPTION 90 </div> </div> </div>

I think this solution in javascript can resolve your problem.我认为 javascript 中的这个解决方案可以解决您的问题。 You have this kind of structure你有这种结构

           <div id="options-container">
                <div class="container">
                  <div class="box">
                    <div class = "a"> OPTION 12 </div>
                   </div>
                </div>
                <div class="container">
                  <div class="box">
                    <div class = "b"> OPTION 78 </div>
                  </div>
                </div>
                <div class="container">
                  <div class="box">
                    <div class = "c"> OPTION 56 </div>
                  </div>
                </div>
                <div class="container">
                  <div class="box">
                    <div class = "d"> OPTION 90 </div>
                  </div>
                </div>
            </div>

I have given id options-container to the parent div.我已经给父 div 提供了 id options-container Then applied this solution然后应用这个解决方案

var options = document.getElementById('options-container');
              var childElements = options.childNodes;
               childElements.forEach(function(child) {
                  if((child.innerText) === "OPTION 56") {
                     child.remove();
                  }
              });              

It dynamically remove every child which will have value OPTION 56 .它会动态删除每个具有OPTION 56值的子项。 I hope it will help you ;)我希望它会帮助你;)

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

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