2012-09-11 69 views
1

您好我想根據屏幕的大小來設置iframe的css高度,但我不知道如何做到這一點。使用javascript設置iframe的高度

CSS:

#iframeplate{ 
    width:100%; 
    height:200px; 
} 

HTML:

<body onload="setGround()"> 
    <iframe id="iframeplate" class="idIframe" frameborder=0 border=0 width="100%" scrolling-y="no" src="#"> 
    </iframe> 
</body> 

JS:

<script type="text/javascript"> 

function setGround() { 
    var groundElement = document.getElementById('iframeplate'); 
    groundElement.style.height = getWindowHeight() + 'px'; 
} 
} 

</script> 

任何人都可以明白爲什麼這是行不通的?或者有更好的方法來做到這一點?

+0

你有沒有看任何的右側類似的問題? – mplungjan

+0

你的JS代碼中有一個額外的「}」。你的函數「getWindowHeight()」是否返回一些東西? – khomyakoshka

回答

3

錯誤是方法getWindowHeight,沒有輸出。

試試這個:

groundElement.style.height = window.screen.availHeight + 'px'; 
+0

是啊對了getWindowHeight沒有輸出window.innerHeight做了這份工作謝謝你把我的思路正確的感謝 – 1ftw1

0

,您可以嘗試

<body onload="setGround()"> 
<iframe id="iframeplate" class="idIframe" frameborder=0 border=0 width="100%" scrolling-y="no" src="#" 
    </iframe> 

使用jQuery像

$("iframe[id='iframeplate']").height(getWindowHeight()); 
1

語法錯誤。試試這個:

<iframe id="iframeplate" class="idIframe" frameborder=0 border=0 width="100%" scrolling-y="no" src="#"></iframe> 


<script type="text/javascript"> 
function setGround() { 
    var groundElement = document.getElementById('iframeplate'); 
    groundElement.style.height = getWindowHeight() + 'px'; 
} 
</script> 
+0

你有兩個「}」括號。 – Solo

+0

是不是,但你在哪裏權利,謝謝你沒有看到那裏:) – 1ftw1