如何使用querySelectorAll选择删除元素的某些属性?

时间:2014-04-03 20:21:29

标签: javascript highcharts

我在这里得到了正确的元素,当我查看样式时,属性是不会删除的,我的方法错了吗?我该怎么做?谢谢!

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');
            }

1 个答案:

答案 0 :(得分:1)

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

例如

on html(<div data-test="test" id="test"></div>

<强> JS

document.getElementById('test').removeAttribute('data-test');

您要找的是删除样式属性,所以就像

document.getElementById('test').style.width="";

更新,查看您的q,您可能希望删除所有样式。如果是的话 -

例如

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

<强> JS

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