IE 에서 동적 으로 iframe 을 만 드 는 두 가지 문제
2842 단어 JavaScript
동적 생 성 iframe 에 폼 제출
문제 설명
다음 코드 는 현재 페이지 에서 제출 폼 을 새로 고치 지 않 고 제출 하 는 데 사 용 됩 니 다. 그 원 리 는 폼 의 target 을 페이지 의 한 iframe id 로 설정 하여 이 iframe 을 제출 하 는 목표 로 삼 아 새로 창 을 열거 나 뛰 지 않도록 하 는 것 입 니 다.그러나 이 코드 는 IE 6, 7 에서 유효 하지 않 습 니 다.
<form action="http://www.baidu.com/" method="post" target="testframe">
<input type="submit" value="Submit" />
</form>
<script>
var iframe = document.createElement('iframe');
iframe.id = 'testframe';
iframe.name = 'testframe';
document.body.insertBefore(iframe, document.body.firstChild);
</script>
해결 방안
innerHTML 방식 으로 iframe 을 삽입 합 니 다. 예 를 들 어:
<script>
var div = document.createElement('div');
div.innerHTML = '<iframe id="testframe" name="testframe" />';
document.body.insertBefore(div, document.body.firstChild);
</script>
동적 으로 iframe 을 만 드 는 onload 이벤트
문제 설명
다음 코드 는 페이지 에 iframe 을 만 들 고 iframe 에 onload 이벤트 리 셋 을 연결 합 니 다. 그러나 이 리 셋 은 IE 6, 7, 8 에서 모두 유효 하지 않 습 니 다.
<script>
var iframe = document.createElement('iframe');
iframe.id = 'testframe';
iframe.name = 'testframe';
iframe.src = 'http://www.baidu.com/';
iframe.onload = function() { alert('onload'); };
document.body.insertBefore(iframe, document.body.firstChild);
</script>
해결 방안
attachEvent 인터페이스 로 이벤트 리 셋 을 연결 합 니 다. 예 를 들 어:
<script>
var iframe = document.createElement('iframe');
iframe.id = 'testframe';
iframe.name = 'testframe';
iframe.src = 'http://www.baidu.com/';
var fn = function() { alert('onload'); };
if (iframe.attachEvent) {
iframe.attachEvent('onload', fn);
} else {
iframe.onload = fn;
}
document.body.insertBefore(iframe, document.body.firstChild);
</script>
다음 글 에 도 관심 이 있 을 수 있 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
기초 정리 - 1문자 (String) 숫자 (Number) 불린 (Boolean) null undefined 심볼 (Symbol) 큰정수 (BigInt) 따옴표로 묶어 있어야 함 Not-A-Number - 숫자 데이터 / 숫자로 표...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.