当前位置: 代码迷 >> JavaScript >> js获取子元素解决办法
  详细解决方案

js获取子元素解决办法

热度:124   发布时间:2013-01-23 10:44:49.0
js获取子元素
举个例子:
<div id='a'  style="width: 300px; height: 500px; background-color: rgb(0, 0, 0);">
     <div id='b' style="width: 800px; height: 300px; background-color: rgb(255, 0, 255);">
         <div id='c' style="width: 100px; height: 100px; background-color: rgb(255, 0, 255);">
 </div>
     </div>
 </div>


怎么通过id为a元素获取到id为b的div的width?

document.getElementById('a').firstChild.css('width') 怎么不对啊

------解决方案--------------------

for(var i in document.getElementById('a').childNodes){
if(document.getElementById('a').childNodes[i].nodeType==1){
console.log(document.getElementById('a').childNodes[i].style.width);
}
}

------解决方案--------------------
$('#a').children().first().width()
  相关解决方案