如何使用 querySelectorAll 的 select 删除元素的某些属性

How do I remove some attributes of an element using selection by querySelectorAll?

本文关键字:属性 元素 select 何使用 querySelectorAll 删除      更新时间:2023-09-26

我在这里得到了正确的元素,当我查看样式时,属性没有被删除,我的方法错了吗?如何正确操作?谢谢!

var axes = document.querySelectorAll('.highcharts-axis-labels');
       for (var i = 0; i < axes.length; i++) {
           if (i == 1) {
               axes[i].removeAttribute("position");
                axes[i].removeAttribute("height");                    
                axes[i].removeAttribute("top");
                axes[i].removeAttribute("width");
                axes[i].removeAttribute("left");
                axes[i].removeAttribute("margin-top");                  
                //axes[i].css('margin-top', '0px');
            }

.removeAttribute()是删除 html 元素的属性

例如

在 HTML ( <div data-test="test" id="test"></div>

.JS

document.getElementById('test').removeAttribute('data-test');
<小时 />

您正在寻找的是删除样式属性,因此会像

document.getElementById('test').style.width="";
<小时 />

更新 ,查看您的 q,您可能希望删除所有样式的内容。㞖-

例如

在 HTML ( <div id="test" style="position:absolute;top:10px;left:10px">test</div>

.JS

document.getElementById('test').removeAttribute('style');