So I'm trying to dynamically build a table in javascript and I already have the jQuery library loaded so I chose to use it's append method because it's easy to use and easy to find my div.
This works in FireFox and Chrome but fails in IE8 (no errors in IE8, the table just doesn't show up)
$("#weatherFeed").append('<table class=\"weather\"><tr>');
for (var i = 0; i < 7; i++) {
$("#weatherFeed").append('<td>a</td>');
}
$("#weatherFeed").append('</tr><tr>');
for (var i = 0; i < 7; i++) {
$("#weatherFeed").append('<td>b</td>');
}
$("#weatherFeed").append('</tr></table>');
Yet this works fine in all three:
$("#weatherFeed").append('<table class=\"weather\"><tr><td>wth</td><td>wth</td>'
+ '<td>wth</td><td>wth</td></tr><tr><td>wth</td>开发者_C百科;<td>test</td><td>test</td>'
+ '<td>test</td></tr><tr><td colspan=\"4\">MOOSE BALLS</td></table>');
It's our corporate intranet and 99% of our users are on IE8.. I'm new to javascript and jQuery but I'm positive I can find another way of accomplishing this goal I just figure it should work so maybe I'm doing it wrong.
jQuery automatically does the closing tags so you dont need to do it yourself.
Also you should append to the table and not to the div.
Try this instead:
var table = $('<table class=\"weather\">');
$("#weatherFeed").append(table);
var tr = $('<tr>');
table.append(tr);
for (var i = 0; i < 7; i++) {
tr.append('<td>a</td>');
}
tr = $('<tr>');
table.append(tr);
for (var i = 0; i < 7; i++) {
tr.append('<td>b</td>');
}
See fiddle: http://jsfiddle.net/maniator/cUkFa/
I would suggest that you step through your code in the debugger. You can bring this up in IE by hitting F12 and going to the Script tab. Once you're there, you can set up a watch for $("#weatherFeed").html(). Under IE7 mode, I stopped after the appending of the first TD and I found that the watch displayed the following:
"<TABLE class=weather>\r\n<TBODY>\r\n<TR>\r\n<DIV></DIV></TR></TBODY></TABLE>\r\n<TD>a</TD>"
This says to me that the tags are being closed for you and that any subsequent appending after the initial TABLE and TR are appearing outside of the table you were trying to create.
精彩评论