【发布时间】:2009-04-03 14:33:50
【问题描述】:
我有一种情况,我在同一个 asp.net 页面上有多个列表框控件。它们目前已自动调整大小以防止数据被截断。但是,我希望能够确定最大列表框的宽度,然后将其他列表框的大小更改为相同。
问题是...如何访问列表框的大小?或者我可以吗?
【问题讨论】:
标签: asp.net javascript vb.net
我有一种情况,我在同一个 asp.net 页面上有多个列表框控件。它们目前已自动调整大小以防止数据被截断。但是,我希望能够确定最大列表框的宽度,然后将其他列表框的大小更改为相同。
问题是...如何访问列表框的大小?或者我可以吗?
【问题讨论】:
标签: asp.net javascript vb.net
是的,你可以...
//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;
【讨论】:
为获得最佳结果,请考虑使用 javascript 框架,使用 jquery:
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);
【讨论】: