當前位置:編程學習大全網 - 網站源碼 - js寫入的iframe自適應高度問題(急)

js寫入的iframe自適應高度問題(急)

1.js控制iframe

<div id="dd">132</div>

<script>

var s = '<iframe src="javascript:document.open();document.write(\'<script>alert(1);<\/script><div>test</div>\');document.close();"></iframe>';

document.getElementById("dd").innerHTML = s

</script>

<iframe id="tst" name="tst" ></iframe>

<script>

alert(2);

var cw = window.document.getElementById('tst').contentWindow;

cw.document.open();

cw.document.write('<script>alert("test")<' + '/script>');

cw.document.write('<div>test</div>');

cw.document.close();

</script>

IE6,IE7,FF2下測試通過

2.自適應高度

在id為"ifr"的iframe內容裏寫上js代碼:

<script>

function resize(){

parent.document.getElementById('ifr').style.height = document.body.scrollHeight>300?document.body.scrollHeight:300+"px";

}

window.onload=resize;

window.onresize = resize;

</script>

就能控制id為"ifr"高度至少為300px

要是回答的內容有問題,或認為不妥,請發送百度消息給我,消息內容加上本頁網址哦。。

·

  • 上一篇:戰神引擎點擊npc沒反應
  • 下一篇:[React] 子組件向父組件通信:回調函數
  • copyright 2024編程學習大全網