我有一个固定宽度的 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>