将行插入表中
在 IE 中将 X 行插入表中的最佳纯 JavaScript 方法是什么?
表 html 如下所示:
<table><tbody id='tb'><tr><td>1</td><td>2</td></tr></tbody></table>
我需要做的是删除旧的正文,并插入一个包含 1000 行的新正文。 我有 1000 行作为 javascript 字符串变量。
问题是IE中的table没有innerHTML功能。 我见过很多黑客可以做到这一点,但我想看看你最好的一个。
注意:使用 jquery 或任何其他框架不算。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是一篇由在 他如何让 IE 执行
tbody.innerHTML="..."
:顺便说一句,他使用的技巧基本上是所有框架如何对
table
/tbody
元素执行此操作。编辑:@mkoryak,你的评论告诉我你的想象力为零,不值得得到答案。 但无论如何我都会逗你开心。 您的积分:
> 他没有插入我需要的东西
什么? 他正在将行(作为 html 字符串)插入到
table
元素中。> 他还使用了一个额外的隐藏元素。
该元素的目的是说明 IE 需要的只是一个“上下文”。 您可以使用动态创建的元素 (
document.createElement('div')
)。> 而且这篇文章已经很旧了,
我再也不会帮助你了;)
但是说真的,如果你想了解其他人是如何实现它的,请查看
jQuery.clean()
,或 Prototype 的Element._insertionTranslations
。Here's a great article by the guy who implemented IE's
innerHTML=
on how he got IE to dotbody.innerHTML="<tr>..."
:Incidentally the trick he uses is basically how all the frameworks do it for
table
/tbody
elements.Edit: @mkoryak, your comment tells me you have zero imagination and don't deserve an answer. But I'll humor you anyway. Your points:
> he is not inserting what i need
Wha? He is inserting rows (that he has as an html string) into a
table
element.> he also uses an extra hidden element
The point of that element was to illustrate that all IE needs is a "context". You could use an element created on the fly instead (
document.createElement('div')
).> and also the article is old
I'm never helping you again ;)
But seriously, if you want to see how others have implemented it, take a look at the jQuery source for
jQuery.clean()
, or Prototype'sElement._insertionTranslations
.像 jQuery 那样做,例如。 在其周围添加
和
,插入到文档中并将所需的节点移动到所需的位置,然后放弃临时元素。
Do as jQuery does it, eg. add
<table>
and</table>
around it, insert into document and move the nodes you want to where you want them and ditch the temp-element.代码最终是这样的:
预先存在的东西:一个表,其主体的 id 为“bodyId”。 $body 是一个全局变量(或者函数上有一个闭包),并且其中也有一些 jquery,因为 IE 不会评估动态插入 html 中的脚本。
the code ended up being this:
Things that beed to exist beforehand: a table that has a body with id of 'bodyId'. $body is a global variable (or the function has a closure on it), and there is a bit of jquery in there too, because IE does not evalute scripts that are inserted into the html on the fly.
我也遇到了同样的问题(就像很多其他人一样),经过大量的尝试后,我开始工作了。
您必须通过 document.createelement ('tr') 创建一个 tr,然后以相同的方式创建一个 td。
将 td 附加到 tr,将 tr 附加到 tbody(不是表),然后您可以将您创建的 td 嵌入到 innerhtml 中,它将起作用。
这是我用的ie8。 基本上,表格结构必须使用 createelement 来制作,但其余部分可以是innerHTMLed。 我在 IE8 调试器中执行此操作,它会说它会添加它(如果我执行了 tr.innerhtml="blah")并且没有给出错误,但它不会显示,并且 html dom 视图显示非常损坏 所以当我最终通过 createelement 调用执行 tr AND td 时,它
创建了一个看起来正确的 dom 并正确绘制了页面。
I had the same problem (as do lots of other people) and after a lot of playing around here's what I got to work.
You have to make a tr via document.createelement ('tr') then make a td, the same way.
appendchild the td to the tr, appendchild the tr to tbody (not table) THEN you can innerhtml the td you created and it will work.
This was ie8 I was using. Basically the table structure has to be made with createelement but the rest of it can be innerHTMLed. I was doing this watching in the IE8 debugger and it would say it would add it (if I did tr.innerhtml="blah") and give no error, but it wouldn't display, and the html dom view showed a very broken table (no td ever showed up, but the /td did)
So when finally I did the tr AND td by createelement calls, it created a correct looking dom and drew the page correctly.