我有一个固定宽度的div,其表格比div宽。 我想做的是缩小表格文本,使其适合div。
根据This Page将宽度设置为百分比设置表格宽度为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>
答案 0 :(得分: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 here