IE6-IE9中tbody的innerHTML不能赋值bug
IE6-IE9中tbody的innerHTML不能赋值,重现代码如下
<!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <title>IE6-IE9中tbody的innerHTML不能复制bug</title> </head> <body style="height:3000px"> <table> <tbody> <tr><td>aaa</td></tr> </tbody> </table> <p> <button id="btn1">GET</button><button id="btn2">SET</button> </p> <script> var tbody = document.getElementsByTagName('tbody')[0] function setTbody() { tbody.innerHTML = '<tr><td>bbb</td></tr>' } function getTbody() { alert(tbody.innerHTML) } btn1.onclick = function() { getTbody() } btn2.onclick = function() { setTbody() } </script> </body> </html>
两个按钮,第一个获取tbody的innerHTML,第二个设置tbody的innerHTML。
获取时所有浏览器都弹出了tr的字符串,但设置时IE6-9不支持,而且报错,如图
可以利用特性判断来看浏览器是否支持tbody的innerHTML设值
var isupportTbodyInnerHTML = function () { var table = document.createElement('table') var tbody = document.createElement('tbody') table.appendChild(tbody) var boo = true try{ tbody.innerHTML = '<tr></tr>' } catch(e) { boo = false } return boo }() alert(isupportTbodyInnerHTML)
相关推荐
WebVincent 2019-11-17
MrWhite0 2015-03-17
srchange 2016-04-26
htmlman0 2019-04-03
DolphinThinker 2019-06-29
kbkiss 2019-06-29
zwq 2016-04-26
WCschool官方 2015-07-20
lzn 2015-06-10
XaverSun 2015-03-17
sunnylinner 2014-12-19
zhangpeng 2014-09-05
来老师 2014-08-10
zhangpeng 2014-05-04
XaverSun 2013-11-15
careyourself 2013-11-15
maozhuofan 2019-06-18
tianqi 2013-07-23