【问题标题】:getElementsByClassName and setAttribute not workinggetElementsByClassName 和 setAttribute 不起作用
【发布时间】:2013-03-08 19:50:57
【问题描述】:

我正在尝试使用 getElementsByClassName 使用 java 脚本更改按钮的样式。它似乎不起作用。 Firebug 给了我一个“没有足够的参数 - list[index].setAttribute("button");”错误。

我的代码如下所示:

function clear(y){
var list, index;
list = document.getElementsByClassName('button1);
for (index = 0; index < list.length; ++index) {
list[index].setAttribute("button");
}
}

任何帮助将不胜感激。谢谢。

【问题讨论】:

    标签: getelementsbyclassname


    【解决方案1】:

    错误是不言自明的。您在方法setAttribute() 中缺少一个参数。为您希望新的button 属性添加另一个参数。

    list[index].setAttribute('button', 'myValue');
    

    如果您打算删除button 属性,请使用removeAttribute()

    list[index].removeAttribute('button');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-21
      • 1970-01-01
      • 2014-09-15
      相关资源
      最近更新 更多