2

我有一个固定宽度的 div,它的表格比 div 宽。我想做的是缩小表格文本,使其适合 div。

根据此页面将表格宽度设置为 100% 将宽度设置为百分比不起作用。

以下代码将起作用,但我如何自动化它,即计算 FontSize?

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
     <link rel="stylesheet" href=/themes/base/jquery.ui.all.css">
     <script src="/jquery-1.7.1.js"></script>    
    <script src="/ui/jquery.ui.core.js"></script>    
    <script src="/ui/jquery.ui.widget.js"></script>    
    <script src="/ui/jquery.ui.mouse.js"></script>    
<script src="/ui/jquery.ui.resizable.js"></script>    
<style>
  #narrorColumn{
    width: 250px;
  }
</style>

<script type="text/javascript">
 function ShrinkTable(){
var FontSize = 6;

function ShrinkTable(){
   var TabWidth = $("#tbl").width();
   var DivWidth= $("#narrowColumn");
   if (DivWidth <= TabWidth)
    {
      $("#tbl").css('font-size', FontSize);
    }
}


</script>

</head>
<body onLoad="ShrinkTable()" >
<div id = "narrorColumn" >
<table id="tbl" border="10">
<thead>
<tr>
<th>First Column</th>
<th>Second Column</th>
<th>Third Column</th>
<th>Forth Column</th>
</tr>
</thead>
<tbody>
<tr>
<td>Some Random text here</td>
<td>Some Random text here</td>
<td>Some Random text here</td>
<td>Some Random text here</td>
 </tr>
<tr>
<td>Mary Had a little lamb</td>
<td>Mary Had a little lamb</td>
<td>Mary Had a little lamb</td>
<td>Mary Had a little lamb</td>
</tr>
<tr>
<td>Humpty Dumpty sat on a wall</td>
<td>Humpty Dumpty sat on a wall</td>
<td>Humpty Dumpty sat on a wall</td>
<td>Humpty Dumpty sat on a wall</td>
</tr>
<tr>
<td>Hay diddle diddle the kat and the fiddle</td>
<td>Hay diddle diddle the kat and the fiddle</td>
<td>Hay diddle diddle the kat and the fiddle</td>
<td>Hay diddle diddle the kat and the fiddle</td>
</tr>
</tbody>
</table>
</div>
<div> <input type=button value="Make Big" onClick="$('#tbl').makeBig()">
</div>
</body>

</html>
4

1 回答 1

1

我认为上面的代码行不通。它有一些拼写错误。

我不确定这是否会对性能产生很大影响,但我认为您可以尝试减小字体大小,直到表格大小小于表格宽度。

见下面的代码:

<div id = "narrorColumn" >
<table id="tbl" border="10">
<thead>
<tr>
<th>First Column</th>
<th>Second Column</th>
<th>Third Column</th>
<th>Forth Column</th>
</tr>
</thead>
<tbody>
<tr>
<td>Some Random text here</td>
<td>Some Random text here</td>
<td>Some Random text here</td>
<td>Some Random text here</td>
 </tr>
<tr>
<td>Mary Had a little lamb</td>
<td>Mary Had a little lamb</td>
<td>Mary Had a little lamb</td>
<td>Mary Had a little lamb</td>
</tr>
<tr>
<td>Humpty Dumpty sat on a wall</td>
<td>Humpty Dumpty sat on a wall</td>
<td>Humpty Dumpty sat on a wall</td>
<td>Humpty Dumpty sat on a wall</td>
</tr>
<tr>
<td>Hay diddle diddle the kat and the fiddle</td>
<td>Hay diddle diddle the kat and the fiddle</td>
<td>Hay diddle diddle the kat and the fiddle</td>
<td>Hay diddle diddle the kat and the fiddle</td>
</tr>
</tbody>
</table>
</div>
<div> <input type=button value="Make Big" onClick="$('#tbl').makeBig()">
</div>​

CSS

#narrorColumn{
    width: 300px; /* You can play around with this width to test it */
    background-color:#ccc;
}

#tbl {
width:100%;
    font-size:55px;
}
​

javascript

function ShrinkTable() {
    var FontSize = parseInt($("#tbl").css('font-size').replace('px', ''),10);
    var TabWidth = $("#tbl").width();
    var DivWidth = $("#narrorColumn");
    /****REMOVED equal sign(=) ****/
    if (parseInt(DivWidth.css('width').replace('px', ''),10) < TabWidth) {
        $("#tbl").css('font-size', FontSize - 4 + 'px'); /* you can change 4 with any number, the smaller is better but it may require more loop */
        //Shrink the font while div width is less than table width
        ShrinkTable();
    }
}

$(document).ready(function() {
    ShrinkTable();
});​

我希望它能回答你的问题。在这里查看 jsfiddle

于 2012-05-08T18:07:48.570 回答