站长资源网络编程

IE6/IE7/IE8/IE9中tbody的innerHTML不能赋值的完美解决方案

整理:jimmy2025/1/11浏览2
简介IE6/IE7/IE8/IE9中tbody的innerHTML不能赋值,重现代码如下复制代码 代码如下: IE6-IE9中tbody的innerH</div> <div class="news_infos"><div id="MyContent"><P>IE6/IE7/IE8/IE9中tbody的innerHTML不能赋值,重现代码如下</P> <P><U>复制代码</U> 代码如下:<BR><!DOCTYPE html><BR><html><BR>    <head><BR>        <meta charset="utf-8"/><BR>        <title>IE6-IE9中tbody的innerHTML不能复制bug</title><BR>    </head><BR>    <body style="height:3000px"><BR>        <table><BR>            <tbody><BR>                <tr><td>aaa</td></tr><BR>            </tbody><BR>        </table><BR>        <p><BR>            <button id="btn1">GET</button><button id="btn2">SET</button><BR>        </p><BR>        <script><BR>            var tbody = document.getElementsByTagName('tbody')[0]<BR>            function setTbody() {<BR>                tbody.innerHTML = '<tr><td>bbb</td></tr>'<BR>            }<BR>            function getTbody() {<BR>                alert(tbody.innerHTML)<BR>            }<BR>            btn1.onclick = function() {<BR>                getTbody()<BR>            }<BR>            btn2.onclick = function() {<BR>                setTbody()<BR>            }<BR>        </script><BR>    </body><BR></html><BR></P> <P>两个按钮,第一个获取tbody的innerHTML,第二个设置tbody的innerHTML。</P> <P>获取时所有浏览器都弹出了tr的字符串,但设置时IE6-9不支持,而且报错,如图</P> <P><a href="javascript:;" onclick="showimg('/UploadFiles/2021-04-02/20146485153257.png?2014548525');"><img src="/UploadFiles/2021-04-02/20146485153257.png?2014548525" alt="IE6/IE7/IE8/IE9中tbody的innerHTML不能赋值的完美解决方案" onmousewheel="return bbimg(this)" onload="javascript:resizepic(this)" border="0"/></a></P> <P>可以利用特性判断来看浏览器是否支持tbody的innerHTML设值</P> <P><U>复制代码</U> 代码如下:<BR>var isupportTbodyInnerHTML = function () {<BR>    var table = document.createElement('table')<BR>    var tbody = document.createElement('tbody')<BR>    table.appendChild(tbody)<BR>    var boo = true<BR>    try{<BR>        tbody.innerHTML = '<tr></tr>'<BR>    } catch(e) {<BR>        boo = false<BR>    }<BR>    return boo<BR>}()<BR>alert(isupportTbodyInnerHTML)<BR></P> <P>对于IE6-IE9里如果要设置tbody的innerHTML,可以使用如下替代方法</P> <P><U>复制代码</U> 代码如下:<BR>function setTBodyInnerHTML(tbody, html) {<BR>    var div = document.createElement('div')<BR>    div.innerHTML = '<table>' + html + '</table>'<BR>    while(tbody.firstChild) {<BR>        tbody.removeChild(tbody.firstChild)<BR>    }<BR>    tbody.appendChild(div.firstChild.firstChild)<BR>}<BR></P> <P>用一个div来包含一个table,然后删除tbody里的所有元素,最后给tbody添加div的第一个元素的第一个元素,即div>table>tr。</P> <P>当然还有一个更精简的版本,它直接采用replaceChild方法替换</P> <P><U>复制代码</U> 代码如下:<BR>function setTBodyInnerHTML(tbody, html) {<BR>    var div = document.createElement('div')<BR>    div.innerHTML = '<table>' + html + '</table>'<BR>    tbody.parentNode.replaceChild(div.firstChild.firstChild, tbody)<BR>}<BR></P> <P>从MSDN上记录上看 col、colGroup、frameset、html、head、style、table、tfoot、tHead、title和tr的innerHTML都是只读的(IE6-IE9)。</P> <P>The innerHTML property is read-only on the col, colGroup, frameSet, html, head, style, table, tBody, tFoot, tHead, title, and tr objects.</P> <P>You can change the value of the title element using the document.title property.</P> <P>To change the contents of the table, tFoot, tHead, and tr elements, use the table object model described in Building Tables Dynamically. However, to change the content of a particular cell, you can use innerHTML.<BR></P></div> </div> </div> <div class="share"> </div> <div class="nextinfo"> <p>上一篇:<a href="http://m.wwsws.com/show/1/163857.html" title="使用css实现全兼容浏览器的三角形">使用css实现全兼容浏览器的三角形</a></p> <p>下一篇:<a href="http://m.wwsws.com/show/1/163859.html" title="详细分析css float 属性以及position:absolute 的区别">详细分析css float 属性以及position:absolute 的区别</a></p> </div> <div class="otherlink"> <h2>最新资源</h2> <ul> <li> <i class="iconfont icon-point"></i> <a class="text-sm" href="/show/1/623322.html" one-link-mark="yes" title="群星《奔赴!万人现场 第2期》[FLAC/分轨][518.87MB]"><span>群星《奔赴!万人现场 第2期》[FLAC/分轨][518.8</span></a> </li> <li> <i class="iconfont icon-point"></i> <a class="text-sm" href="/show/1/623321.html" one-link-mark="yes" title="群星《奇妙浪一夏 (上海迪士尼度假区音乐)》[320K/MP3][43.91MB]"><span>群星《奇妙浪一夏 (上海迪士尼度假区音乐)》[32</span></a> </li> <li> <i class="iconfont icon-point"></i> <a class="text-sm" href="/show/1/623320.html" one-link-mark="yes" title="群星《奇妙浪一夏 (上海迪士尼度假区音乐)》[FLAC/分轨][140.49MB]"><span>群星《奇妙浪一夏 (上海迪士尼度假区音乐)》[FL</span></a> </li> <li> <i class="iconfont icon-point"></i> <a class="text-sm" href="/show/1/623319.html" one-link-mark="yes" title="【古典音乐】詹姆斯·高威《季节》1993[WAV+CUE]"><span>【古典音乐】詹姆斯·高威《季节》1993[WAV+CUE]</span></a> </li> <li> <i class="iconfont icon-point"></i> <a class="text-sm" href="/show/1/623318.html" one-link-mark="yes" title="贝拉芳蒂《卡里普索之王》SACD[WAV+CUE]"><span>贝拉芳蒂《卡里普索之王》SACD[WAV+CUE]</span></a> </li> <li> <i class="iconfont icon-point"></i> <a class="text-sm" href="/show/1/623317.html" one-link-mark="yes" title="小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]"><span>小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]</span></a> </li> <li> <i class="iconfont icon-point"></i> <a class="text-sm" href="/show/1/623316.html" one-link-mark="yes" title="群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]"><span>群星《欢迎来到我身边 电影原声专辑》[320K/MP3</span></a> </li> <li> <i class="iconfont icon-point"></i> <a class="text-sm" href="/show/1/623315.html" one-link-mark="yes" title="群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]"><span>群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨</span></a> </li> <li> <i class="iconfont icon-point"></i> <a class="text-sm" href="/show/1/623314.html" one-link-mark="yes" title="雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]"><span>雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓</span></a> </li> <li> <i class="iconfont icon-point"></i> <a class="text-sm" href="/show/1/623313.html" one-link-mark="yes" title="群星《2024好听新歌42》AI调整音效【WAV分轨】"><span>群星《2024好听新歌42》AI调整音效【WAV分轨】</span></a> </li> </ul> </div> </div> <div class="sidebar"> <div class="cloud"><h2 class="hometitle">一句话新闻</h2><a href="/3G/1/604673.html"><ul>微软与英特尔等合作伙伴联合定义“AI PC”:键盘需配有Copilot物理按键<br><br>几个月来,英特尔、微软、AMD和其它厂商都在共同推动“AI PC”的想法,朝着更多的AI功能迈进。在近日,英特尔在台北举行的开发者活动中,也宣布了关于AI PC加速计划、新的PC开发者计划和独立硬件供应商计划。<br>在此次发布会上,英特尔还发布了全新的全新的酷睿Ultra Meteor Lake NUC开发套件,以及联合微软等合作伙伴联合定义“AI PC”的定义标准。</ul></a></div> </div> </article> <footer> <p>友情链接:<a href="http://www.imxmx.com/" title="杰晶网络" target="_blank">杰晶网络</a> <a href="http://www.ddrfans.com/" title="DDR爱好者之家" target="_blank">DDR爱好者之家</a> <a href="http://www.nqxw.com/" title="南强小屋" target="_blank">南强小屋</a> <a href="http://www.paidiu.com/" title="黑松山资源网" target="_blank">黑松山资源网</a> <a href="http://www.dyhadc.com/" title="白云城资源网" target="_blank">白云城资源网</a> <a href="/sitemap.xml">SiteMap</a></p> <p>Design by <a href="http://m.wwsws.com">伏龙阁资源网</a> <a href="/">http://m.wwsws.com</a></p> </footer> <script src="/images/nav.js"></script> <script type="text/javascript"> jQuery.noConflict(); jQuery(function() { var elm = jQuery('#left_flow2'); var startPos = jQuery(elm).offset().top; jQuery.event.add(window, "scroll", function() { var p = jQuery(window).scrollTop(); jQuery(elm).css('position', ((p) > startPos) ? 'fixed' : ''); jQuery(elm).css('top', ((p) > startPos) ? '0' : ''); }); }); </script> </body> </html>