http://www.sweetvision.com/2007/04/08/dynamic-tables-in-javascript-for-ie-and-firefox/
Recently I had the “pleasure” of dynamic creating some tables in
JavaScript. In the process, I ran into and interesting issue or two.
The first issue is that you cannot just append <tr> elements into
a <table> element in IE
. It will work, but IE
will not render the rows. In IE
you must create a <tbody> element, append the <tbody>
element to the <table> element, then append your rows to your
<tbody> element. This does not follow the W3C Row groups specification
for the table specification which states that: “Table rows may be
grouped into a table head, table foot, and one or more table body
sections, using the THEAD, TFOOT and TBODY elements, respectively.”
Note that the W3C specification states ‘may be’ whereas IE
treats it as “must be.”
So, the following will get you nothing in IE
but will work fine in Firefox
:
var myTable = document.createElement("table");
var myRow = document.createElement("tr");
var myCell = document.createElement("td");
myCell.innerHTML = "Hello World!";
myTable.appendChild(myRow);
myRow .appendChild(myCell);
This will render a table in both IE
and Firefox
:
var myTable = document.createElement("table");
var myTbody = document.createElement("tbody");
var myRow = document.createElement("tr");
var myCell = document.createElement("td");
myCell.innerHTML = "Hello World!";
myTable.appendChild(myTbody);
myTbody.appendChild(myRow);
myRow.appendChild(myCell);
So now we have a table that renders in both IE
and Firefox
but there is still an issue remaining. Now in Firefox
there is a space between the top of the table and the first row that
you cannot get rid of. This is not going to be evident in all
situations but will be in enough situations to be a problem. The reason
for the space is that when you add a <tbody> to a table in Firefox
it
appears to reserve or auto include the <thead> and <tfoot>
elements in the table. This is most likely done because the W3C
specification states that you must have a <thead> and
<tfoot> if you have a <tbody>. In order to prevent this
extra space you need to add the<thead> and the <tfoot> and
set their height to 0px or their display to none or something of the
sort unless, of course, you are going to use them.
var myTable = document.createElement("table");
var myThead = document.createElement("thead");
myThead.style.height = 0px;
var myTfoot = document.createElement("tfoot");
myTfoot.style.height = 0px;
var myTbody = document.createElement("tbody");
var myRow = document.createElement("tr");
var myCell = document.createElement("td");
myCell.innerHTML = "Hello World!";
myTable.appendChild(myThead);
myTable.appendChild(myTfoot);
myTable.appendChild(myTbody);
myTbody.appendChild(myRow);
myRow.appendChild(myCell);
This gives you a way to render tables that match in both IE
and Firefox
with the same code.
分享到:
相关推荐
partial function application, and dynamic functionsoffline detection and storing data on the client machinetechniques for JavaScript in an enterprise environment for better maintainability ...
JavaScript is a high-level, dynamic, untyped, lightweight, and interpreted programming language and functional programming is a style that emphasizes and enables smarter code that minimizes complexity...
Spring Dynamic Modules in Action is a comprehensive tutorial that presents OSGi concepts and maps them to the familiar ideas of the Spring framework. In it, you'll learn to effectively use Spring DM. ...
In this paper, we present a novel method to fuse observations from an inertial measurement unit (IMU) and visual sensors, such that initial conditions of the inertial integration, including gravity ...
about data structures and algorithms written in JavaScript. This was strange to me because today many of the job opportunities for software development require knowledge of JavaScript; it is the only ...
dynamic control of active and reactive current injection during faults, and grid services support.This book explains the topologies, modulation and control of grid converters for both photovoltaic ...
Dynamic web sites are flexible and potent creatures, more accurately described as applications than merely sites. Dynamic web sites : - Respond to different parameters (for example, the time of day ...
Beginning JavaScript with DOM Scripting and Ajax is an essential resource for modern JavaScript programming. This completely updated second edition covers everything you need to know to get up-to-...
Beginning JavaScript with DOM Scripting and Ajax is an essential resource for modern JavaScript programming. This completely updated second edition covers everything you need to know to get up-to-...
近似动态规划方法,百度apollo2.0系统里planning模块...(Optimal Trajectory Generation for Dynamic Street Scenarios in a Frene´t Frame Moritz Werling, Julius Ziegler, So¨ren Kammel, and Sebastian Thrun)
JavaScript for Kids is a lighthearted introduction that teaches programming essentials through patient, step-by-step examples paired with funny illustrations. You’ll begin with the basics, like ...
If you know the basics of HTML, JavaScript, and CSS, you’ll be building apps in no time.” —August Trometer, Owner of FoggyNoggin Software, www.foggynoggin.com Discover the Easier, Faster Way to ...