我有以下脚本来设置 height
在 div
上包含 Google map 控件:
<script>
var height = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ? document.documentElement.clientHeight : screen.height;
//alert("Height:" + height)
document.getElementById('divMap').setAttribute("style", "width:" + height);
</script>
再往下,div
定义:
<div id="divMap" class="col-md-6" style="min-height: 800px; height:100%">
<map:GoogleMap ID="GoogleMap1" runat="server" Width="100%" Height="100%">
</map:GoogleMap>
<map:GoogleMarkers ID="GoogleMarkers1" TargetControlID="GoogleMap1" runat="server" OnClick="GoogleMarkers1_Click" />
</div>
该脚本在加载时运行良好。我得到 height
浏览器窗口。
然而,getElementById()
总是失败。
就像div
脚本运行时不存在。如果我选择调试页面,无非就是<script>
显示标签。
我错过了什么,但是什么?
或者有另一种方法来设置 height
的 div
在 asp.net 中?
最佳答案
我认为问题是您试图在 div 进入 DOM 之前访问它。试试这个:
<html>
<head>
</head>
<body>
<div id="divMap" class="col-md-6" style="min-height: 800px; height:100%">
<map:GoogleMap ID="GoogleMap1" runat="server" Width="100%" Height="100%">
</map:GoogleMap>
<map:GoogleMarkers ID="GoogleMarkers1" TargetControlID="GoogleMap1" runat="server" OnClick="GoogleMarkers1_Click" />
</div>
<script>
(function() {
/var height = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ? document.documentElement.clientHeight : screen.height;
//alert("Height:" + height)
document.getElementById('divMap').setAttribute("style", "width:" + height);
})();
</script>
</body>
</html>
关于javascript - document.getElementById 在 asp.net 中的 <div> 上失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52327971/