【问题标题】:Get size of autosized control?获取自动控制的大小?
【发布时间】:2009-04-03 14:33:50
【问题描述】:

我有一种情况,我在同一个 asp.net 页面上有多个列表框控件。它们目前已自动调整大小以防止数据被截断。但是,我希望能够确定最大列表框的宽度,然后将其他列表框的大小更改为相同。

问题是...如何访问列表框的大小?或者我可以吗?

【问题讨论】:

    标签: asp.net javascript vb.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;
    

    【讨论】:

    • too much comment use -> //获取最大宽度 \n var maxW = Math.max(widths);
    • 我知道,但我希望所有 Javascript 新手都能理解。
    • 应该说这是JavaScript,所以运行客户端,/not/在服务器上。
    • @Richard:他的问题被标记为“Javascript”
    • 这正是我想要的。谢谢!
    【解决方案2】:

    为获得最佳结果,请考虑使用 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);
    

    【讨论】:

    • 一个简单的任务不应该需要一个大的库。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-05
    • 2011-07-18
    • 2021-02-28
    • 1970-01-01
    • 1970-01-01
    • 2014-08-30
    相关资源
    最近更新 更多