代码之家  ›  专栏  ›  技术社区  ›  Woppi

YUI Dom.getChildren公司

  •  0
  • Woppi  · 技术社区  · 14 年前

    当类是动态的并且只有父类有id时,是否可以更改不带id的类?

    我有点像:

    <div id="number_block">
      <div class="one science easy left"></div>
      <div class="one science easy center"></div>
      <div class="one science easy right"></div>
    </div>
    

    我只够到这部分

    var number_block_children = Dom.getChildren('number_block');
    for(var i=0; i < number_block_children.length; i++)
    {
         /* I don't know the syntax to change class name here for every child, is it possible?
          * I can't use Dom.getElementByClassName...since the class is dynamic.
          * It's something similar to how get classname by id, only I don't have id, just parent id:
          *        Dom.get('id-name-here').className
          * I can't figure out how to do this....
          */
    
    }
    

    谢谢!

    2 回复  |  直到 14 年前
        1
  •  1
  •   jira    14 年前

    可以使用getAttribute获取元素类:

    var number_block_children = YAHOO.util.Dom.getChildren('number_block');
    for(var i=0; i < number_block_children.length; i++)
    {
    
        var class = YAHOO.util.Dom.getAttribute(number_block_children[i], 'class');
        var classes = class.split(' ');
    
    }
    
        2
  •  0
  •   jebberwocky    14 年前

    嘿你可以做点什么

    YAHOO.util.Event.addListener(window, "load", function() {
        var number_block_children = YAHOO.util.Dom.getChildren('number_block');
        for(var i=0; i < number_block_children.length; i++)
        {
            console.log(YAHOO.util.Dom.hasClass(number_block_children[i], 'one'));
            console.log(YAHOO.util.Dom.hasClass(number_block_children[i], 'two'));
        }
    });
    

    推荐文章