2013-06-13 155 views
0

我想使地图的高度与设备的屏幕自动... 但当我将style.height更改为自动,100%,screen.height,地图消失... 如何使高度为自动? 这是我的代码...style.height does not work javascript

的script.js

$('#home').live('pagecreate',function(){ 
document.getElementById('map_canvas').style.width=screen.width; 
document.getElementById('map_canvas').style.height="20em"; 
}); 

index.html中我只是叫

<div id="home" data-role="page"> 
    <div data-role="header"> 
    *my header* 
    </div> 

    <div data-role="content"> 
     <div id="map_canvas"></div> 
    </div> 
</div> 
+0

#map_canvas为第一,而不是map_canvas –

+0

如果我使用#map_canvas,脚本将无法正常工作......这就是为什么我没有使用# –

+0

你的js调试器是否与你一致?尝试$('#map_canvas').css('height','auto'); –

回答

0

如果你使用jquery你可以如下操作: ,而不是这个:

document.getElementById('map_canvas').style.width=screen.width; 
document.getElementById('map_canvas').style.height="20em"; 

您可以使用:

$("#map_canvas").css({ 
    'width':screen.width, 
    'height':"20em" 
}); 
+0

非常感谢你....它的作品...我想高度是自动的...所以我改变高度为“screen.height”没有“...但格式是这样的..... –

0

请检查这个example: 我认为你的问题与CSS不相关的JavaScript。 要设置将根据家长的高度,所以%的高度,如果父母是空的,0 100%为0:check this

代码示例:

<!DOCTYPE html> 
<html> 
    <head> 
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script> 
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script> 
<body> 
<style> 
#map_canvas{ 
    background-color:#00F; 
    } 
#cont{ 
    background:#F00; 
    height:200px; 
    } 
</style> 
<button onClick="doit1()">Click me to put something in map_canvas</button> 
<button onClick="doit()">Click me to make the height 100%</button> 

<div id="home" data-role="page"> 
    <div data-role="header"> 
    *my header* 
    </div> 

    <div id="cont" data-role="content"> 
     <div id="map_canvas"></div> 
    </div> 
</div> 
</body> 
<script> 
$('#home').live('pagecreate',function(){ 
$('#map_canvas').css('height','auto'); 
}); 
function doit(){ 
    $('#map_canvas').css('height','100%'); 
    } 
function doit1(){ 
    $('#map_canvas').html('a') 
    } 
</script> 
</html>