2016-12-17 3 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> 

はなぜcaは、それが働いている200

答えて

0

を示しているではない、クラスrを通じて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未定義 – Anjali

0

あなたのコードこのおもちゃを追加しようと、希望divの現在の高さ表示される必要があります。

HTML:

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

はJavaScript:

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さんは未定義です – Anjali

+0

このスニペットを実行しましたか? –

+0

あなたはこれを見ましたか\t w3schools.com/code/tryit.asp?filename=FATMX1JOJKP1 – Anjali

関連する問題