2015-04-22 55 views
0

我有一个id为“myframe”的iframe我想在设备旋转时更改高度。这是这样的设备旋转时如何更改iframe的大小?

<script> 
$(document).on("pagecreate",function(event){ 

$(window).on("orientationchange",function(){ 
    if(window.orientation == 0) 
    { 

    document.getElementById("myframe").height = 300; 

    } 
    else 
    { 
    document.getElementById("myframe").height = 500; 

    } 
});     
}); 
</script> 

,但是需要这样

<script> 
$(document).on("pagecreate",function(event){ 

$(window).on("orientationchange",function(){ 
    if(window.orientation == 0) 
    { 
    var oldh = document.getElementById("myframe").height; 

    var newh = parseInt(oldh) * 1.4; 
    if(newh != ''){ 
    document.getElementById("myframe").height = newh; 
    } 
    } 
    else 
    { 
    var oldh = document.getElementById("myframe").height; 
    var newh = parseInt(oldh)/1.4; 

    if(newh != ''){ 
    document.getElementById("myframe").height = newh; 
    } 
    } 
});     
}); 
</script> 

工作,如果我打印的VAR newh它正确返回结果,但仍然无法运行我的功能

+0

你试过用css设置高度吗? 'document.getElementById('myframe')。style.height = newh +'px';' –

+0

此外,您的计算可能会导致浮点数。你要做这样的:'VAR newh = parseInt函数(oldh/1.4);' –

+0

你说得对有关parseInt函数格式它工作得很好。谢谢:)如果你把它作为一个答案,我将迎来它正确。 – Nato

回答

1

你的计算很可能导致一个浮点数。你必须这样做: var newh = parseInt(oldh/1.4);

相关问题