2012-02-16 7 views
0

javascriptで同じ高さの関数を作成したい、私はgetElementsByNameメソッドを使用してそれを取得するが、それは動作していないようだ、私はdivの最大の高さを選択し、同じidを持つ他のdivsを適用したいループはjavascriptで動作していませんか?

equl高機能

<script type="text/javascript"> 

function equalHeight() { 

var get= document.getElementsByName('jj'); 

for (i=0;i<get.length;i++){ 

    var m = get[i].offsetHeight; 
    alert (m) 

    var n= Math.max(m); 




    document.getElementsByName('jj').style.height= n +"px"; 

} 






    } 

window.onload = equalHeight; 




</script> 

</head> 

<body> 
<div style="margin:0 auto; width:500px;"> 
<div style="border:solid #F00 1px; float:left; width:150px" name="jj">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</div> 

<div name="jj" style="border:solid #F00 1px; float:right; width:150px">Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32. 

The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.</div> 



</div> 
</body> 
</html> 
+0

、あなたが等しくなるようにしたいものを、あなたのdiv要素のすべてにクラス名を追加し、すべての最初の、これを試してみてくださいループの内部で 'get.style.height = n +" px ";"として参照できると思っていたでしょう。 – Wez

+0

はhttps://developer.mozilla.org/ja/JavaScript/Reference/Global_Objects/Math/maxを見てください。 '.max()'は0以上の数字をとります。毎回1つの数字を返します。 – Matijs

+0

A DIVはNAME属性を持つべきではありません。 IEはあなたのDIVと 'getElementsByName'をマッチさせません。なぜなら、IEはIDだけを見てNAMEでは見ないからです。それをIDに変更すると、他のブラウザはNAMEだけを見るので一致しません。無効な属性を使用しているNAME + IDをまだ追加している場合は、そのIDで要素を取得する別の方法を選択してください。 –

答えて

0

私は "equalHeight" 機能があるべきだと思う:name属性をサポートしていませDIV、あなたは親のdiv内のすべてのdivを入れて、「ID DIV親を与えることができます

var get = document.getElementsByTagName('div'), 
    maxHeight = 0, i; 

for (i = 0; i < get.length; i++) { 

    var m = get[i].offsetHeight; 
    if (m > maxHeight) { 
     maxHeight = m; 
    } 
} 

for (i = 0; i < get.length; i++) { 

    get[i].style.height = maxHeight + "px"; 
} 

「ちょうどこのように:その後、

<div id="parent_div"> 
    <div></div> 
    <div></div> 
</div> 

divを取得:

var get = document.getElementById("parent_div").getElementsByTagName("div"); 
+0

偉大なる先生、私は珍しい –

-1
for (i=0 ; i<get.length;i++){ 
    var m = get[i].offsetHeight; 
    alert (m) // your are missing an ; here 
    var n= Math.max(m); 
    document.getElementsByName('jj').style.height= n +"px"; 
} 
+0

私はちょうどそれが値を表示しているかどうかをテストしていましたが、私は再びそれが動作していないことを確認します –

1

問題は、あなたがあなたに新しい高さを割り当てる同じ要素の高さを得ることです。最初に最大の高さを取得する必要があります。それをすべてのdivに割り当てます。 JSFiddle Demo

function equalHeight() { 
    var divElements = document.getElementsByName('jj'), 
     maxHeight = 0, 
     i = 0; 

    for (i = 0; i < divElements.length; i += 1) { 
     maxHeight = divElements[i].offsetHeight > maxHeight ? divElements[i].offsetHeight : maxheight; 
    } 

    for (i = 0; i < divElements.length; i += 1) { 
     divElements[i].style.height = maxHeight + 'px'; 
    } 
} 
+0

そこに最大の高さを取得するanthor方法です。 –

0

すでにループIの前に名前で要素を取得しているので、すなわち、クラス=「JJ」

function equalHeight() 
{ 
    var el=document.getElementsByTagName("div"); 
    var maxHeight=0; 
    for(var i=0;i<el.length;i++) 
    { 
     if(el[i].className=='jj') 
     maxHeight = el[i].offsetHeight > maxHeight ? el[i].offsetHeight : maxHeight; 
    } 
    for(var i=0;i<el.length;i++) 
    { 
     if(el[i].className=='jj') 
     el[i].style.height = maxHeight + 'px'; 
    } 
} 
window.onload = equalHeight; 
+0

私はgetElementsByClassNameはtagnameではないと言いました。 –

+0

ああ、大変申し訳ありません。 すべてのdivをループしてクラスをチェックする方法が特に効率的であるかどうかは不明です。しかし、誰も、とにかく、最近JQueryなしでこのようなことをしていますか? – 5arx

+0

私自身もそれを求めています。 –

関連する問題