자바 스크립트 .. document.write를 .. 이상한 문서 동작 '(

2011-08-15 2 views
0

나는 매우 이상한 행동을 받고 있어요 :(나는 누군가가 문서와 자바 스크립트 파일을 받고 XMLHTTP 요청 메신저를 사용자바 스크립트 .. document.write를 .. 이상한 문서 동작 '(

을 도울 수 있기를 바랍니다 그것과 물품. .이 같은 같습니다 내가 doc.getElementsByTagName('input');을 얻을 때 지금

document.write("<input type='hidden' name='orange' value='17' />"); 
document.write("<input type='hidden' name='apple' value='29' />"); 
나는 기본적으로 iframe 내에있는 형태로 그 입력 요소를 추가 할

// i get the document of the iframe 
var docc = doc.getElementById("rs-iframe").contentDocument; 
var body = docc.getElementsByTagName('body')[0]; 

// i put the response from xmlhttprequest in a script tag 
var script = docc.createElement('script'); 
script.type = "text/javascript"; 
script.text = "//<![CDATA["+xmlhttp.responseText+"//]]>"; 

// i get the position where i want to put my script tag 
var elem = form.getElementsByTagName('script')[6]; 

// i try to insert my script tag from xmlhttprequest before the script i retrieve from the form 
elem.parentNode.insertBefore(script, elem); 

// the i append the form to the body of the iframe document      
body.appendChild(form); 

를 내가 document.write에서 추가 된 요소 만 가져오고 다른 폼 요소는 사라졌습니다. (

감사합니다.

답변

4

이것이 바로 write()이하는 일이며, 문서 안에 씁니다. 문서가 이미 닫혀있는 경우 (닫힌 경우 완전히로드 됨), write()는 이 문서을 덮어 씁니다.

해결 방법은 간단합니다. 문서가 이미로드 된 경우 write()를 사용하지 마십시오. 새로운 노드를 삽입하려면 appendChild() 또는 insertBefore()와 같은 DOM 메소드를 사용하십시오.

 관련 문제

  • 관련 문제 없음^_^