[英]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
查找类的第一个实例。 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.