0

有没有办法用 JS 更改所有按钮的类值?现在我有:

  var nodes = document.getElementsByClassName("yui-button yui-radio-button");

然后是一个 for 循环

  for (i=0;i<nodes.length;i++) {
     nodes[i].className = "something";
  }

有没有绕过for循环并更改所有类值?

这是html:

    <div id="season" align="center">
     <div id="seasonButtons" class="yui-buttongroup">
      <span id="yui-gen0" class="yui-button yui-radio-button">
       <span class="first-child">
        <button type="button" id="yui-gen0-button">Spring</button>
       </span>
      </span>
      <span id="yui-gen1" class="yui-button yui-radio-button">
       <span class="first-child">
        <button type="button" id="yui-gen1-button">Summer</button>
       </span>
      </span>
      <span id="yui-gen2" class="yui-button yui-radio-button">
       <span class="first-child">
        <button type="button" id="yui-gen2-button">Fall</button>
       </span>
      </span>
     </div>
    </div>
4

4 回答 4

2

如果您只是使用普通的 javascript,那么您需要某种循环来更改数组中的多个节点。

您可以为自己编写一个辅助函数,该函数将在数组中的所有节点上运行,但在某个地方会有一个循环运行。

由于看起来您正在使用 YUI,因此 YUI 可能具有通过一次调用对一组节点进行操作的方法。

于 2013-05-10T06:30:49.693 回答
0

为所有 div 添加一个公共类。例如 class="common"

然后

$('.common').addClass('newClassName');

于 2013-05-10T07:14:39.913 回答
0

NodeList getElementsByClassName(字符串类名):

返回具有类属性的元素的类数组对象,该类属性包括所有指定的 classNames.classNames。

如果您想使用 javascript,您需要一个循环来更改该节点的属性。

于 2013-05-10T08:33:58.143 回答
0

你可以用jquery做到这一点

    $(".yui-button,.yui-radio-button").addClass("something").removeClass("yui-button yui-radio-button");
于 2013-05-10T07:34:45.190 回答