2013-06-13 154 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>