Javascript style.height不起作用

Javascript style.height不起作用,javascript,android,css,cordova,Javascript,Android,Css,Cordova,我想使地图的高度是自动与设备的屏幕。。。 但是当我将style.height更改为auto,100%,screen.height时,地图就消失了。。。 如何使高度自动? 这是我的密码 script.js $('#home').live('pagecreate',function(){ document.getElementById('map_canvas').style.width=screen.width; document.getElementById('map_canvas').style

我想使地图的高度是自动与设备的屏幕。。。 但是当我将style.height更改为auto,100%,screen.height时,地图就消失了。。。 如何使高度自动? 这是我的密码

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>

*我的头球*

如果使用jquery,可以执行以下操作: 与此相反:

document.getElementById('map_canvas').style.width=screen.width;
document.getElementById('map_canvas').style.height="20em";
您可以使用:

$("#map_canvas").css({
    'width':screen.width,
    'height':"20em"
});
请检查以下内容: 我认为你的问题与css有关,而与javascript无关。 要设置的高度百分比将基于父高度,因此如果父高度为空,则0的100%将为0:

代码示例:

<!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>

#地图画布{
背景色:#00F;
}
#续{
背景:#F00;
高度:200px;
}
单击我在地图画布中放置一些内容
单击我使高度达到100%
*我的头球*
$('#home').live('pagecreate',function()){
$('map_canvas').css('height','auto');
});
函数doit(){
$('map_canvas').css('height','100%);
}
函数doit1(){
$('#map_canvas').html('a'))
}

#首先是map#u canvas,而不是map#u canvas如果我使用#map#u canvas,脚本将无法工作…这就是为什么我没有使用#您的js调试器同意您的意见吗?试试$('map'u canvas').css('height','auto');是的……没有。。。我尝试了$('map_canvas').css('height','auto');它仍然与使用屏幕相同。高度…地图不显示…但当我将高度设置为“20em”时,它可以工作…可能还有一些其他规则需要考虑该元素。非常感谢…它可以工作…我想高度是自动的…所以我把高度改为“screen.height”,没有“…但是格式是这样的。。。。。