Вы можете использовать все виды дополнительной разметки для спагетти или добавить один класс к вашей таблице, например так:
<table class="FunkifyMyBackgounds">
<tr>
<th>Heading 1</th>
<th>Heading 2</th>
</tr>
<tr>
<td>...</td>
<td>...</td>
</tr>
</table>
и использовать очень простой jQuery javascript:
<script type="text/javascript">
function SetAllSpecialCellBackgrounds (bNeedToCreateStructure) {
var zCellsToBackgroundify = $(".FunkifyMyBackgounds td");
//--- Set each cell's funky background.
zCellsToBackgroundify.each (function (idx) {
SetA_SpecialCellBackground ($(this), idx, bNeedToCreateStructure);
} );
}
function SetA_SpecialCellBackground (zJnode, K, bNeedToCreateStructure) {
if (bNeedToCreateStructure) {
//--- Add our special background structure.
var sIdName = 'idSpecialCellBG_Container' + K;
zJnode.append (
'<div id="' + sIdName + '" class="SplitCellBackground">'
+ '<div class="TopOfCell"> <\/div><div class="BottomOfCell">'
+ ' <\/div><\/div>'
);
}
ResizeA_SpecialCellBackground (zJnode);
}
function ResizeA_SpecialCellBackground (zJnode) {
var zCellBG_Frame = zJnode.find ('div.SplitCellBackground');
//--- Set the background container to match the cell dimensions.
zCellBG_Frame[0].style.width = zJnode.outerWidth (false) + 'px';
zCellBG_Frame[0].style.height = zJnode.outerHeight (false) + 'px';
//--- Position absolutely; Adjust for margin, if needed.
var aContentPos = zJnode.offset ();
//--- Redundant for IE. Tested and IE really seems to need it.
zCellBG_Frame[0].style.top = aContentPos.top + 'px';
zCellBG_Frame[0].style.left = aContentPos.left + 'px';
zCellBG_Frame.offset (aContentPos);
}
$(document).ready ( function () {
SetAllSpecialCellBackgrounds (true);
/*--- Globally catch table cell resizes caused by the browser window
change.
A cross-browser, good-enough solution is just to use a timer.
Keep it just under a second per usability guidelines.
*/
$(window).resize (function() {SetAllSpecialCellBackgrounds (false);} );
setInterval (function() {SetAllSpecialCellBackgrounds (false);}, 444);
} );
</script>
Требуется CSS:
/***** Start of split-cell, specific styles. ****
*/
.SplitCellBackground, .TopOfCell, .BottomOfCell {
margin: 0;
padding: 0;
width: 100%;
height: 50%;
z-index: -10;
}
.SplitCellBackground {
position: absolute;
width: 10em;
height: 10em;
}
.TopOfCell {
background: #33FF33;
}
.BottomOfCell {
background: #FF33FF;
}
/***** End of split-cell, specific styles. *****/
Вы можете увидеть полный код в действии на jsBin .
Работает со всеми основными браузерами, но не с IE6.