Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
293 views
in Technique[技术] by (71.8m points)

javascript - Why does .html work and not innerHTML or appendChild

I am trying to add an ajax response to a div (it's HTML code with tables, forms, etc).

In FF innerHTML works perfectly, but in IE it gives me an unknown error.

I tried lots of stuff, but I only got it working when I added jQuery and ran the .html method on the div I want the code inserted into.

Anyone care to explain why this works and not a simple innerHTML? I tried looking at the .html() code, but I guess I am not the great at JS because I didn't understand what it was doing.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

IE has several documented (pre | table (thead, tbody, tr, tfoot) | div | select) .innerHTML bugs.

Thus libraries like jQuery abstract away those bugs for you by applying workarounds where needed for IE.

As for your specific error... without seeing the code it is hard to tell.

Setting the .innerHTML on pre's, certain div's, select's (if it does fail, will fail silently) but setting the .innerHTML on certain table elements (in certain versions of IE) will actually throw an error/exception.

Note: The issue with setting the .innerHTML of a div is very specific in condition and only occurs in IE6 & IE7.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...