日期:2014-05-16  浏览次数:20389 次

js怎么获取这几个div??
部分代码:
<div id="hp_wrapper" class="chn-wrapper" style="width:968px;">
   
  <div class="nav">
  <div id="div1">
  <asp:ImageButton ID="Img1" runat="server" onclick="Img1_Click"/>
  </div>
  <div id="div2">
  <asp:ImageButton id="Img2" runat="server" onclick="Img2_Click" />
  </div>

  </div>
   
  <div class="style2" >
 
  <asp:Label id="LA1" Font-Size="12" runat="server" />
 
  <div style="display:inline-table; float:left;"><br />
  <asp:ImageButton runat="server" Width="170" Height="190" ID="A1" onclick="A1_Click" />
  </div>
   
  </div>
</div>

我要获取第二层容器div(即class="nav",class="style2"的div)用来实现div旋转?
但js里面我这样写:
var oimages = document.getElementById("hp_wrapper").getElementsByTagName("div")没反应,该怎么获取?

------解决方案--------------------
既然你都给每个div起了id,怎么不直接获取呢
------解决方案--------------------
JScript code
//这样也可以筛选出直接子节点,不管class属性是否为空
var wrapper = document.getElementById('hp_wrapper');
var obj = document.getElementById('hp_wrapper').getElementsByTagName('div');
var oimages = [];
for (var i = 0; i < obj.length; i ++) if (obj[i].parentNode == wrapper) oimages.push(obj[i]);
alert(oimages.length); //2

------解决方案--------------------
JScript code
function getDiv() {
    var pardiv = document.getElementById("hp_wrapper");
    var childs = pardiv.childNodes;
    for (var i = 0; i < childs.length; i++) {
        if (childs[i].className == 'style2') 
            return childs[i];
    }
}