【问题标题】:getElementsByClassName not effecting multiple divsgetElementsByClassName 不影响多个 div
【发布时间】:2015-11-29 22:27:53
【问题描述】:
<a id="backgroundenamel_realbutton_powderblue"
 href="javascript:set_radio('radio_bgenamel_powderblue');" 
 class="radio-picture-enamel" style="background-color: #97b4d2;"
 onclick="document.getElementsByClassName('cx00ringbuilder_topinsidecolor_img')[0].style.backgroundColor='#97b4d2';">&nbsp;</a> 

以上内容应该改变多个 div 的颜色。我需要一种方法来完成所有的 div。我知道我必须将 [0] 更改为数组或其他内容,但我不知道如何....任何帮助将不胜感激。

【问题讨论】:

  • 阅读thisthisthis
  • 我还要补充一点,使用内联 JS 不利于代码的可维护性。分离 HTML 和 JS 使调试更容易。

标签: javascript html css background-color getelementsbyclassname


【解决方案1】:

您不能一次为多个对象赋值。循环遍历对象:

var elements = document.getElementsByClassName('cx00ringbuilder_topinsidecolor_img');
for (var i = 0; i < elements.length; i++) {
  elements[i].style.backgroundColor='#97b4d2';
}

【讨论】:

    猜你喜欢
    • 2019-04-26
    • 1970-01-01
    • 2017-12-07
    • 1970-01-01
    • 2016-12-24
    • 1970-01-01
    • 2013-03-30
    • 2023-01-07
    • 1970-01-01
    相关资源
    最近更新 更多