Dynamic Tables In JavaScript for IE and Firefox

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值