Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/89.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使表格中的所有单元格具有相同的宽度,等于最宽的单元格宽度_Javascript_Html_Css - Fatal编程技术网

Javascript 使表格中的所有单元格具有相同的宽度,等于最宽的单元格宽度

Javascript 使表格中的所有单元格具有相同的宽度,等于最宽的单元格宽度,javascript,html,css,Javascript,Html,Css,在不使用固定宽度的情况下,表格中所有单元格的宽度是否可能与最宽单元格的宽度相同。是的 <table> <tr> <td>short</td> <td>longer</td> <td>the longest cell</td> </tr> </table> 短的 比较长的 最长的牢房 var max=0, $c

在不使用固定宽度的情况下,表格中所有单元格的宽度是否可能与最宽单元格的宽度相同。

是的

<table>
    <tr>
        <td>short</td>
        <td>longer</td>
        <td>the longest cell</td>
    </tr>
</table>

短的
比较长的
最长的牢房
var max=0,
$cells=$('td');
$cells.each(函数(){
var width=$(this.width();
最大=最大<宽度?宽度:最大;
});
$cells.each(函数(){
$(此).width(最大值);
});

[编辑]

正如@connexo在评论中指出的,当表大于其最大大小时,需要更多的逻辑来处理这种情况:

var max = 0,
    $cells = $('td');

$cells.each(function () {
    var width = $(this).width();
    max = max < width ? width : max;
});

$table = $cells.closest('table');

if ($table.width() < max * $cells.length) {
    max = 100 / $cells.length + '%';
}

$cells.each(function () {
    $(this).width(max);
});
var max=0,
$cells=$('td');
$cells.each(函数(){
var width=$(this.width();
最大=最大<宽度?宽度:最大;
});
$table=$cells.closest('table');
if($table.width()

[编辑]

这是一个基于ECMA5的版本,不需要jQuery:

var max = 0,
    cells = document.querySelectorAll('td');

Array.prototype.forEach.call(cells, function(cell){
    var width = cell.offsetWidth;
    max = max < width ? width : max;
});

var table = document.querySelector('table'),
    uom = 'px';

if (table.offsetWidth < max * cells.length) {
    max = 100 / cells.length;
    uom = '%';
}

Array.prototype.forEach.call(cells, function(cell){
    cell.setAttribute('style','width:' + max + uom);
});
var max=0,
单元格=document.querySelectorAll('td');
Array.prototype.forEach.call(单元格,函数(单元格){
变量宽度=cell.offsetWidth;
最大=最大<宽度?宽度:最大;
});
var table=document.querySelector('table'),
计量单位=‘px’;
if(表偏移网络宽度<最大*单元长度){
最大值=100/单元长度;
计量单位='%';
}
Array.prototype.forEach.call(单元格,函数(单元格){
cell.setAttribute('style','width:'+max+uom);
});

只有在有足够的可用空间时,此功能才起作用。如果没有,内容较少的单元格将忽略您应用的宽度。我不明白“它”在您的评论中指的是什么。我的意思是,当然,它只在有足够的可用空间时才起作用。。。OP在这种情况下不需要任何特殊行为:)没有必要“在这种情况下需要特殊行为”,因为他们明确表示需要创建等宽列的解决方案。他们没有提到可用的水平分辨率,这一事实仅在您的解释中使要求无效。
var max=0,$cells=$('td'),$table=$cells.closest('table')$每个(函数(){var width=$(this.width();max=maxvar max = 0,
    cells = document.querySelectorAll('td');

Array.prototype.forEach.call(cells, function(cell){
    var width = cell.offsetWidth;
    max = max < width ? width : max;
});

var table = document.querySelector('table'),
    uom = 'px';

if (table.offsetWidth < max * cells.length) {
    max = 100 / cells.length;
    uom = '%';
}

Array.prototype.forEach.call(cells, function(cell){
    cell.setAttribute('style','width:' + max + uom);
});