innerHTML form ¿À·ù(IE °è¿¸¸) - ÇØ°áÃ¥
innerHTML form ¿À·ù(IE °è¿¸¸)
µ¿ÀûÀ¸·Î ÆäÀÌÁö¸¦ ±¸¼ºÇϱâ À§ÇÏ¿© innerHTMLÀ» »ç¿ëÇÏ°Ô µÇ´Âµ¥,
FF °è¿Àº ¹®Á¦°¡ ¾øÀ¸³ª IE °è¿¿¡¼ ¹®Á¦°¡
¹ß»ýÇÑ´Ù.
¸¸ÀÏ innerHTML·Î ´ëÀÔÇÏ·Á´Â ¹®ÀÚ¿¿¡ form űװ¡ Á¸ÀçÇÑ´Ù¸é innerHTMLÀº µ¿ÀÛÇÏÁö ¾Ê´Â´Ù.
var
oDiv = document.getElementById("testdiv");
oDiv.innerHTML =
"<form><input
type="text"></form>"; // ¿À·ù ¹ß»ý
oDiv.innerHTML = "<input
type="text">"; // Á¤»ó
ȸÇÇÇÒ ¼ö ÀÖ´Â ¹æ¹ýÀº
¾ø´Ù.
innerHTML·Î oDiv °´Ã¼ ¾È¿¡ ´Ù¸¥ °´Ã¼¸¦ ³Ö¾î¼ ±× °´Ã¼ÀÇ innerHTMLÀ» ÇÏ´õ¶óµµ ¿À·ù°¡ ¹ß»ýÇÑ´Ù.
ºê¶ó¿ìÀúº° ¹ö±× ¹× Â÷ÀÌÁ¡
XHTML : form ÇϺÎÀÇ ³ëµå¿¡ ´ëÇؼ innerHTML ·Î »õ·Î¿î ³ëµå¸¦ »ý¼ºÇÒ ¶§, form À» »ý¼ºÇÒ °æ¿ì '¾Ë ¼ö ¾ø´Â ·±Å¸ÀÓ ¿À·ù' ¹ß»ý
1. ¹ß»ýÇö»ó
form ÇϺÎÀÇ ³ëµå (Á÷Á¢Àû ÇϺΠ¹× 2Depth ÀÌ»óÀÇ ÇϺθ¦ ¸ðµÎ Æ÷ÇÔ) ¿¡ ´ëÇؼ innerHTML ·Î »õ·Î¿î ³ëµå¸¦ »ý¼ºÇÒ ¶§, innerHTML ·Î ´ëÀÔÇÒ ¹®ÀÚ¿ ¾È¿¡ form űװ¡ ¾î´À Depth ¿¡ Á¸ÀçÇϵçÁö°£¿¡ innerHTML ¿¡ ´ëÀÔÇÏ´Â ¼ø°£ '¾Ë ¼ö ¾ø´Â ·±Å¸ÀÓ ¿À·ù' °¡ ¹ß»ýÇÑ´Ù.
* ¿¹
<form>
<div id="xx">
</div>
</form>
...
var node = document.getElementById("xx");
node.innerHTML = '<div id="xx2"><form><input type="text" /></form></div>'; <- ¿À·ù ¹ß»ý
2. ºê¶ó¿ìÀú º° Ư¼º
o. ¹ß»ýÇÏ´Â ºê¶ó¿ìÀú
- Microsoft Internet Explorer 7
o. ¹ß»ýÇÏÁö ¾Ê´Â ºê¶ó¿ìÀú
- Mozilla Firefox 2.0
- Opera 9.x
3. ÇØ°á¹æ¹ý
- ±Ùº»ÀûÀÎ ÇØ°á¹æ¹ýÀº ¾øÀ½
- °¡±ÞÀû ´ÙÀ½ »çÇ×À» ÁöŲ´Ù.
1) form ÇϺΠ³ëµå¿¡ ´ëÇؼ´Â form À» »ý¼ºÇÏ´Â °ÍÀ» ÃÖ´ëÇÑ ÇÇÇÑ´Ù.
2) form ³ëµå¸¦ ÄÄÆ÷³ÍÆ®È ½Ãų ³ëµåÀÇ °æ°èÁ¡À¸·Î µÎÁö ¾Êµµ·Ï ÇÑ´Ù.
==>ÇØ°áÃ¥ ¤»¤»¤»
http://domscripting.com/blog/display/99
Fixing innerHTML
I presented my Brighton Ajax workshop yesterday. It all went rather smoothly. The workshop was in the same swanky building as the Clearleft office so I felt like I was playing on home turf. The homely feel was emphasised with the choice of snacks: Jessica was kind enough to bake up a whole batch of cookies for the workshop attendees. The cookies were a big hit.
During the workshop, I discussed the data formats available for Ajax
responses. There¡¯s XML and JSON, both of which can be parsed and
then added to the document using DOM
methods like createElement
and appendChild
. Another
option is to simply send back HTML from the server and dump it into
the document using innerHTML
. If I ever find myself needing to
update just one part of the page, this is the solution I use.
Suppose the part of the page I want to update is a div
with an
ID of ¡°container¡±. I can just add the HTML that has been sent in the
responseText
property of XMLHttpRequest
:
var container = document.getElementById("container");
container.innerHTML = xhr.responseText;
But innerHTML
can cause some problems, particularly in Internet
Explorer. If you use innerHTML
to add or update form
elements, all sorts of screwiness can occur. Sometimes the data from the newly
added elements won¡¯t be included when the form is submitted to the server.
Toby Cole?a Brighton-based developer working at Semantico?was at the workshop. He proposed an ingenious solution.
First of all, use the DOM to create a block level element?such as a
div
?using createElement
. Then update the
innerHTML
property of this newly-created element. Finally, insert
the updated element into the document using a DOM method like
appendChild
or insertBefore
:
var newdiv = document.createElement("div");
newdiv.innerHTML = xhr.responseText;
var container = document.getElementById("container");
container.appendChild(newdiv);
Ta-da! The best of both worlds: the brevity and speed of
innerHTML
but without the problems in IE.
See, this is one of the reasons why I like the intimacy of workshops. It¡¯s a great environment to exchange ideas. I think I¡¯ve learned something new from the attendees at every workshop I¡¯ve presented but this one was particularly fruitful.
Posted by Jeremy on Saturday, March 3rd, 2007 at 7:53pm