2016-12-17 32 views
1

的clientWidth我想則firstChild的客戶寬度如何獲得則firstChild

的CSS

.ot{width:100px;height:200px} 

JS

var ra = document.getElementsByClassName("r")[0]; 
var n = ra.firstChild.clientHeight; 
var ca = n ; 

HTML

<div class="r"><div class="ot"></div></div> 

希望通過r類,爲什麼不ca正顯示出200

回答

0

它正在努力獲得的ot高度。你可以在這裏閱讀更多關於HTML DOM的信息。 http://www.w3schools.com/jsref/dom_obj_all.asp

var ra = document.getElementsByClassName("r")[0]; 
 
var n = ra.firstChild.clientHeight; 
 
var ca = n ; 
 

 
alert(ca);
.ot{width:100px;height:200px}
<div class="r"><div class="ot"></div></div>

+0

w3schools.com/code/tryit.asp?filename=FATMX1JOJKP1 undefined – Anjali

0

嘗試添加這個玩具你的代碼,應該表現出所需div的當前高度:

HTML:

<p id="demo"></p>` 
<button onclick="myFunction()">Try it</button> 

的JavaScript:

function myFunction() { 
var ra = document.getElementsByClassName("r")[0]; 
var n = ra.firstChild.clientHeight; 

document.getElementById("demo").innerHTML = n; 

} 

CSS:

.ot{width:100px;height:200px} 
0

與工作片斷試試,我已修改了代碼,只需要運行這個片段中,你可以用你的第一個孩子身高檢查和寬度

var ra = document.getElementsByClassName('r')[0]; 
 
var n = ra.getElementsByTagName('div')[0]; 
 
alert('width is =' + n.clientWidth + ' , and height = ' + n.clientHeight);
.ot{ 
 
    width:100px; 
 
    height:200px 
 
}
<div class="r"> 
 
    <div class="ot"></div> 
 
</div>

+0

http://www.w3schools.com/code/tryit.asp?filename=FATMX1JOJKP1 undefined – Anjali

+0

你是否運行過這段代碼? –

+0

你看到這個\t w3schools.com/code/tryit.asp?filename=FATMX1JOJKP1 – Anjali