简体   繁体   中英

innerHtml not working in ie for table

I have a table, and in that table i want to add few rows by javascript, but this is not working in ie10.

<table id="itemidentitynamedeploy">
</table>

javascript Code

var itemidentitynamedeploy = document.getElementById('itemidentitynamedeploy');
for(var x=0;x<6;x++)
{
itemidentitynamedeploy.innerHTML += "<tr><td>"+x+"</td></tr>";
}

Please Suggest.

There's wrong order of tr and td enclosing tags ( </tr></td> ). Use this

itemidentitynamedeploy.innerHTML += "<tr><td>"+x+"</td></tr>";

instead of this

itemidentitynamedeploy.innerHTML += "<tr><td>"+x+"</tr></td>";

Try..

 var tr=itemidentitynamedeploy.insertRow(x);
 var td=tr.insertCell(0);
 td.innerHTML=x

Here is a working fiddle (tested in IE10 and Chrome),

http://jsfiddle.net/HjMwJ/

Since the above works with the JavaScript called onload, if you are not getting this working in IE10 in your code, maybe you need to make sure that the JavaScript in your solution is being called once the DOM element is loaded too...

If you're not sure how to do this, you could do it by any of the following options

  • Wrapping your javascript in a $(function() { ... }) if you're using jQuery
  • Putting the <script> tag containing this JavaScript at the end of your <body>
  • Making sure your code fires once the window has loaded... window.onload=function(){ ... };

each of these methods has different subtle lifecycle/timing implications which are described elsewhere on this forum.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM