我有一個固定寬度的div,它的表格比div寬。 我想要做的是縮小表格文字,使其適合div。減少表格中的字體大小,使表格適合格子
將寬度設置爲百分比根據This Page將表寬度設置爲100%不起作用。
下面的代碼將工作,但我如何自動化它,即計算字體大小?
<!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>
hmm ...字體大小是6什麼? 6px,6pt,6em?認爲你需要添加單位 – Rodolfo