简体   繁体   English

我如何在没有ID选择器的情况下删除元素的类-使用普通JavaScript(无jquery)?

[英]How can I remove an element's class without ID selector - with vanilla JavaScript (no jquery)?

First of all I've to say that I'd like to use this code in "tampermonkey"; 首先,我要说的是我想在“ tampermonkey”中使用此代码;
There is a code like this: 有这样的代码:

<div class="tab-pane fade show active" id="tab-content-download" role="tabpanel" aria-labelledby="tab-download">
   <div class="d-none d-js-block">
       <p>...some text...</p>
   </div>
</div>

(I also have to mention that I have disabled Chrome Javascript in this site.) (我还必须提到,我已在此站点中禁用了Chrome Javascript。)
1. Is it possible to use tampermonkey when chrome javascript is disabled? 1.禁用Chrome javascript时是否可以使用tampermonkey?

2.How Can I remove " d-none " class only? 2.如何才能仅删除“ d-none ”课程?

You can find all the elements by class, select the first one (in this example), go to his parent and remove the element you found 您可以按类查找所有元素,选择第一个(在此示例中),转到其父级并删除找到的元素

Note: getElementsByClassName returns an array of elements, even if only 1 element was found. 注意:即使仅找到1个元素, getElementsByClassName也会返回元素数组。

 var el = document.getElementsByClassName("d-none")[0]; el.parentElement.remove(el); 
 <div class="tab-pane fade show active" id="tab-content-download" role="tabpanel" aria-labelledby="tab-download"> <div class="d-none d-js-block"> <p>...some text...</p> </div> </div> 

If you want to remove the class and not the element: 如果要删除类而不是元素:

 var el = document.getElementsByClassName("d-none")[0]; el.classList.remove("d-none"); 
 <div class="tab-pane fade show active" id="tab-content-download" role="tabpanel" aria-labelledby="tab-download"> <div class="d-none d-js-block"> <p>...some text...</p> </div> </div> 

  • getElementsByClassName Find the first instance of class. getElementsByClassName查找类的第一个实例。
  • Remove element using DOM element classList . 使用DOM元素classList删除元素。

 document.getElementsByClassName("d-none")[0].classList.remove("d-none"); 
 <div class="tab-pane fade show active" id="tab-content-download" role="tabpanel" aria-labelledby="tab-download"> <div class="d-none d-js-block"> <p>...some text...</p> </div> </div> 

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

相关问题 如何在没有 jQuery 的情况下删除 Javascript 中的元素 - How can I remove an element in Javascript without jQuery 如何在 jquery on() 中获取选择器 class 元素的 ID - How to get id of element of selector class in jquery on() 如何从jQuery选择器返回的列表中删除元素? - How can I remove an element from a list returned by a jQuery selector? 如何将此 JQuery 转换为香草 Javascript? - How can I convert this JQuery into vanilla Javascript? 在香草JS中,如何根据滚动条是否位于顶部来从元素中添加或删除类? - In vanilla JS, how can I add or remove a class from an element based on whether the scroll is at the top? 如何在没有javascript而没有jquery的情况下将id添加到没有id的元素 - how can I add an html to an element without id with javascript without jquery 滚动到页面上没有jquery的元素(Vanilla Javascript) - Scroll to element on the page without jquery (Vanilla Javascript) 如何判断一个元素是 jquery DOM 元素还是原版? - how can I tell if an element is a jquery DOM element or vanilla? Vanilla Javascript:如果单击具有相同切换类的Y元素,则从X元素中删除切换类 - Vanilla Javascript: Remove toggle class from X element if I click on Y element with the same toggle class 如何使用类选择器覆盖id选择器的jQuery事件处理程序? - How can I override a jQuery event handler of an id selector with a class selector?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM