javascript - document.getElementById 在 asp.net 中的 <div> 上失败

标签 javascript c# asp.net

我有以下脚本来设置 heightdiv 上包含 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>显示标签。

debuging script

我错过了什么,但是什么?

或者有另一种方法来设置 heightdiv在 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/

相关文章:

javascript - VM1990 :5 Uncaught SyntaxError: Unexpected token :

javascript - 返回输入值的最佳方法

c# - 如何设置文本框标题的样式?

c# - c# 中是否有相当于 f'{}' 的函数

c# - 如何以编程方式在 ASP.NET 页面中生成 401 错误

c# - 将linq查询结果存储在Model中

javascript - Meteor 在发布函数中获取用户名

C# 如何禁用 webbrowser 使用

c# - Linq to SQL VS Entity Framework

javascript - 如果我有一个按钮触发 React 中第二个子组件的状态,如何将状态传递给父组件