`
dickyzhu
  • 浏览: 111744 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
文章分类
社区版块
存档分类
最新评论

Table innerHTML -- work around

阅读更多
As many of you must have read or realised(i did) while working with it, MS Internet Explorer doesn't allow you to set the innerHTML property of any table related tag(table, thead, tbody, tr except for td). It says "Unknown Runtime Exception". You are provided with explicit methods to handle this. Like createRow, createCell and stuff... Also you can use W3C DOM append child.

Good way to make tables, but what if I have the Table Head defined in the HTML file and read the Table Body from a XML file as well defined string. I simply want to have this in the table... One way is I parse the string as XML and loop through the nodes creating the cells and so on (that is what I first did), But why do we need that unecessary processing. We don't have any method to parse the string and get into our DOM directly.

The work around I worked out is probably not the only or best, but works good.

suppose we have a variable sXmlBody which has all the table body rows defined like:

var sXmlBody = "<tr><td>cellOne1</td><td>cellTwo1</td><tr>" +
"<tr><td>cellOne2</td><td>cellTwo2</td><tr>" ;


Of course it will be created dynamically ;)

Now to add this to the DOM enclose it in actual table and tbody tags.

sXmlBody = "<table><tbody>" + sXmlBody + "</tbody></table>";

and get it in DOM parsed as HTML using a temp div


var tempDiv = document.createElement("DIV");
tempDiv.innerHTML = sXmlBody;

Now I can use W3C DOM to append the child from the temp div to the table in our document. Consider the id of the table in the HTML document id tableResults


var tableElement = document.getElementById("tableResult");
tableElement.appendChild(tempDiv.firstChild.firstChild);

If you are going to reload the table content, it will be useful to have an id for the tbody tag and use removeChild on the documents table with the tbody element as parameter.

I hope that's helpful to you all, and it works fine in Firefox too!

Till Next time . . . Happy coding
分享到:
评论

相关推荐

    innerHTML-outerHTML-innerTEXT.txt

    根据提供的文件信息,我们可以深入探讨三个重要的DOM属性:`innerHTML`、`innerText`以及`outerHTML`。这些属性在Web开发中非常关键,用于获取或设置元素内部的内容。接下来将详细解析这些属性的功能、用途及差异。 ...

    deck-of-cards-workout:需要锻炼吗? 让命运决定!!!!!!

    - **DOM操作**:利用`document.querySelector`或`document.querySelectorAll`选取页面元素,然后用`innerHTML`或`innerText`更新页面内容。 - **条件语句**:根据卡片的花色和数字执行不同的锻炼动作。 - **可能的库...

    innerHTML的简单应用

    innerHTML的简单应用

    hw-5-work-day-scheduler

    【标题】"hw-5-work-day-scheduler" 是一个项目,可能是一个个人或团队为了学习、实践或展示技能而创建的。从名字来看,它似乎是一个日程管理应用,可以帮助用户规划他们一天的工作时间。 【描述】由于描述部分只给...

    一个用于将JSON数据转换为Table的JavaScript库

    这个函数会返回一个HTML字符串,可以直接插入到页面的某个元素中,如`document.getElementById('table-container').innerHTML = result;`。 3. **自定义样式**:json-table-converter库允许你通过CSS类名来定制表格...

    JS小功能(操作Table--动态添加删除表格及数据)实现代码

    `&lt;table&gt;`用于展示表格数据,按钮用于触发添加新行的操作。 ```html &lt;div style="margin-left:300px;margin-top:30px;"&gt; 种族名称: 种族简称: 英雄: 添加信息"/&gt; &lt;table id="tab1" border="1" style="text-...

    GridView-HtmlTable-Part2.zip

    例如,我们可以使用`document.createElement()`创建新的表格行,`appendChild()`添加元素,以及`innerHTML`属性来设置或获取元素的HTML内容。 总结以上,本项目"GridView-HtmlTable-Part2.zip"旨在教你如何利用...

    控制table经典实例-javascript

    var table = document.createElement('table'); document.body.appendChild(table); ``` 2. **插入行和单元格**:利用`insertRow()`方法在表格中插入新行,`insertCell()`方法插入新单元格。 ```javascript var ...

    IE bug table元素的innerHTML

    首先时飘忽不定的collapse的border,ie6偏偏...排查了半天,结果是IE的table元素为只读,不能使用innerHTML!!!! 同样为只读的元素还有COL, COLGROUP, FRAMESET, HTML, STYLE, TABLE, TBODY, TFOOT, THEAD, TITLE, T

    innerHtml(转)

    1. **innerHTML的基本用法**:如何通过`innerHTML`读取和修改元素的内容。例如,`element.innerHTML`返回元素的HTML,而`element.innerHTML = someHTMLString`则将元素内容替换为指定的HTML字符串。 2. **性能影响*...

    超全面javaweb第4天- 08 innerHTML属性

    超全面javaweb第4天-_08_innerHTML属性

    利用innerHTML实现隐显效果-两种实现方法

    本文将探讨如何利用innerHTML实现页面元素的隐藏与显示效果,同时提供两种不同的实现方法。 首先,我们需要理解innerHTML的工作原理。innerHTML允许我们获取或设置一个元素的所有子节点,包括文本、HTML标签等。当...

    js实现table分页可以动态生成table

    tableBody.innerHTML = ''; pageData.forEach(item =&gt; { const row = document.createElement('tr'); Object.keys(item).forEach(key =&gt; { const cell = document.createElement('td'); cell.textContent = ...

    js-table分页

    - **DOM操作**:使用DOM API(如`innerHTML`或`insertAdjacentHTML`)更新表格内容。 3. **库的使用**: - `jQuery DataTables`:这是一个流行的JavaScript库,提供了强大的表格功能,包括分页、排序、搜索等。 ...

    js插件设置innerHTML时在IE8下提示“未知运行时错误”解决方法

    通常我们采用的方式是设置该DOM元素的innerHTML属性。然而,在特定的浏览器环境下,这种操作可能会引发问题,比如在IE8浏览器中,某些情况下会遇到设置innerHTML时提示“未知运行时错误”的情况。这个问题虽然在现代...

    简单网页table布局

    在table布局中,`&lt;table&gt;`标签用于创建表格,`&lt;tr&gt;`表示表格行,`&lt;td&gt;`定义单元格,`&lt;th&gt;`则用于表头。例如,创建一个简单的2x2表格的HTML代码如下: ```html &lt;table&gt; 列1 列2 行1,列1 行1,列2 &lt;/...

    react-Innerself使用innerHTML将ReactRedux转换成50行代码

    标题中的“react-Innerself使用innerHTML将ReactRedux转换成50行代码”表明这是一个关于简化React和Redux集成的项目,它使用了一种叫做Innerself的技术。Innerself是一种轻量级的解决方案,通过innerHTML属性来实现...

    关于在innerHTML中JS不执行的问题

    首先,我们需要理解`innerHTML`的工作原理。当设置一个元素的`innerHTML`时,浏览器会做以下几件事: 1. 清空当前元素的所有子节点。 2. 解析新设置的HTML字符串,生成一个DOM树。 3. 将新的DOM树插入到原始元素的...

    innerHTML的使用

    innerHTML的使用document.getElementById("id").innerHTML = "contenttext";

    IE6-IE9中tbody的innerHTML不能赋值的解决方法

    IE6-IE9中tbody的innerHTML不能赋值,重现代码如下 Js代码 &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;meta charset=utf-8/&gt; &lt;title&gt;IE6-IE9中tbody的innerHTML不能复制bug&lt;/title&gt; &lt;/...

Global site tag (gtag.js) - Google Analytics