2009-04-03 8 views
1

私は、同じasp.netページにいくつかのリストボックスコントロールがある状況があります。現在、データは自動的に切り捨てられないように自動化されています。しかし、私は最大のリストボックスの幅を決定し、他のリストボックスのサイズを同じに変更することができるようにしたいと思います。自動化されたコントロールのサイズを取得しますか?

質問です...どのようにリストボックスのサイズにアクセスできますか?または私はできますか?最良の結果を得るための

答えて

1

することができますはい...

//This is a <div> that wraps around your listboxes 
var wrapperDiv = document.getElementById('listboxWrapper'); 

//Get all <select> elements within the <div> 
var sels = wrapperDiv.getElementsByTagName('SELECT'); 

//An array to store the width values 
var widths = new Array(); 

//Load the array 
for(var i = 0, l = sels.length; i < l; i++) 
    widths[i] = sels[i].offsetWidth; 

//Get the max width 
var maxW = Math.max(widths); 

//Set the max width to all the list boxes 
for(var sel in sels) 
    sels[sel].style.width = maxW; 
+0

あまりにも多くのコメントを使用 - > //は、最大幅を取得します\ n var maxW = Math.max(widths); – cjk

+0

私は知っていますが、私はすべてのJavascript初心者が理解してほしいです。 –

+0

これはJavaScriptなので、サーバー側でクライアント側、/ not /を実行します。 – Richard

-1

、jqueryのを使用して、JavaScriptのフレームワークを使用することを検討してください:

var width = 0; 
//get the largest width 
$("select").each(function() { 
    if ($(this).width() > width) { 
     width = $(this).width(); 
    } 
}); 
//make them all the same width 
$("select").css("width", width); 
+0

単純なタスクでは大きなライブラリは必要ありません。 –

関連する問題