如何根据asp.net中最长文本得到TableCell的宽度 [英] How to get tablecell width based upon longest text in asp.net

查看:238
本文介绍了如何根据asp.net中最长文本得到TableCell的宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

技术:Visual Studio 2010中,Asp.Net 4.0

Technology: Visual Studio 2010, Asp.Net 4.0

我有几个表在彼此垂直对齐。他们是在codebehind动态创建。我想使第一列的基础上从第一列中的最长文本中的每个表相同的宽度。有知道的宽度设定最初是因为细胞需要动态调整内容。

I have several tables vertically aligned over each other. They are created dynamically in codebehind. I would like to make the first column the same width for each table based on the longest text from first column. there is know width set initially because the cell needs to resize dynamically to content.

注意:每个表行的第一列(TD)具有一个标签。

Note: the first column(td) of each table row has a label.

因此​​,例如:

Table1 (first column)
1
12

Table2 (first column)
123
1234

最长宽度将是一个包含[1234]所述细胞。现在,我想其他表的第一列单元格设置为宽度。

The longest width would be the cell that contains [1234]. Now I would like to set the first column cells of the other tables to that width.

下面是一个基本的布局。 FIDDLE 结果
当然,这并不能说明它是如何动态创建的,但它的总体JIST。

Here is a basic layout. FIDDLE
Of course this doesn't show how it is created dynamically but the general jist of it.

推荐答案

您可以通过TD迭代,并使用文本()函数查找的字符个数,并筛选出TD与最大字符数。

You can iterate through td and use text() function to find the count of characters and filter out the td with maximum number of characters.

现场演示

Live Demo

var maxlen=0;
var tdLongest;
$('table td').each(function(){
    if($(this).text().length > maxlen)
    {
        maxlen = $(this).text().length;
        tdLongest = $(this);
    }
});
alert(tdLongest.text());
alert(tdLongest.width());

这篇关于如何根据asp.net中最长文本得到TableCell的宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆