开发者

html table column width, fix width at time of page creation

If there an easy way to fix the width of the html table columns at the time the page is rendered. I DO NOT want to specify the pixel or percent width of the columns in my code. However, once the page is created and the widths determined based on the initial content in the table I do not want the widths to change, even if the size of the cell content chang开发者_StackOverflow中文版es.

Just so you understand what i'm trying to do, if I have the content of a cell change(from normal to bold for example), say when I hover over the row, the size of the columns will change. This looks quite odd.


To be honest I didn't understand the answer you gave to my comment, but I'll attempt at an answer anyway :-)

What you maybe need is to wrap the content of each cell in a div (or some other block element) and set it's width. That should limit the widening of the cells, however as I hinted at im my comment, the wider content will overflow, which most like look ugly. However you didn't really specify what you want the wider content to do...

<style type="text/css">
    #the-table td .wrap {
       overflow: hidden; /* try different values for different effects */
    }
</style>

<script type="text/javascript">
    $(function(){
        $('#the-table th, #the-table td').wrapInner(function() {
           return $("<div/>").css("width", $(this).width() + "px");
        });
    });
</script>

Live example: http://jsfiddle.net/Vx5Zq/

Notice how the letter F is cut off on hover.


I think this should do the trick (not checked though):

var w = $('#myTable td.leftCol').outerWidth();
$('#myTable td.leftCol').css({width: w+'px'});

[EDIT]

<!DOCTYPE HTML>
<html lang="en-US">
<head>
    <meta charset="UTF-8">
    <title></title>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
    <script type="text/javascript">
        $(document).ready(
            function() {
                $("table td").each(
                    function() {
                        var w = $(this).outerWidth();
                        alert(w+'px');
                        w *= 2;
                        $(this).css({width: w+'px'});
                        alert(w+'px');
                    }
                );
            }
        );
    </script>
</head>
<body>
    <table>
        <tr>
            <td>abc</td>
            <td>def</td>
            <td>ghi</td>
        </tr>
    </table>
</body>
</html>


Adding some width to your current tds will stop the resizing when the the text goes bold.

First set your table using css to fixed rendering:

<style type="text/css">
    #mytable{ table-layout: fixed; }
</style>

Then you can use jquery to add 2px of width to your tds like so:

<script type="text/javascript">
    $(function(){
        $('#mytable td').each(function(){
            var $this = $(this) // cache this td
            // set style to 2px wider
            $this.css({ width: $this.outerWidth() + 2, height: $this.outerHeight() + 2 });
        })
    });
</script>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜