TypeError:无法读取null

时间:2018-09-18 14:21:32

标签: javascript html

尝试从Github添加日历控件。我收到此消息。

TypeError: Cannot read property 'getAttribute' of null
    at n (flatpickr:2)
    at ve (flatpickr:2)
    at ShowBins.aspx?env=*DEV&Elev=S &Name=DALLAS CTR&Site=31:74

HTML被称为ShowBins

  <link rel="stylesheet" href="assets/main.css" />
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/flatpickr/dist/flatpickr.min.css">
  <script src="https://cdn.jsdelivr.net/npm/flatpickr"></script> 
  <script>
      const today = new Date();
      const fp = flatpickr(document.querySelector("#datepicker"), { "maxDate": today });
</script>

</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolderMain" Runat="Server">
       <div class="form-group" style="background-color:white;">
               <asp:label ID="Label3" runat="server" class="auto-style32" Width="97px" >Date Shown:</asp:label>
                <asp:TextBox ID="datepicker" ClientIDMode="Static"  runat="server"  Width="100px" Font-Bold="true"  OnTextChanged="datepicker_OnTextChanged" AutoPostBack="true"></asp:TextBox>
                <asp:label ID="LblBins" text="Bins" runat="server" style="margin-left: 100px"></asp:label>
                <asp:DropDownList ID="DropDownBins" runat="server"  AutoPostBack="true"  OnSelectedIndexChanged="DropDownBins_SelectedIndexChanged" CssClass="auto-style28"  Width="50px" Height="22px" Font-Bold="true"></asp:DropDownList>
                <asp:label ID="LblStdTemp" text="Standard Temperature" runat="server" style="margin-left: 100px "  ></asp:label>
              <asp:TextBox ID="StdTemp"  runat="server" ClientIDMode="Static" CssClass="auto-style24" Width="20px"  OnTextChanged="StdTemp_TextChanged" AutoPostBack="true"  Font-Bold="true"></asp:TextBox>
           </div>

1 个答案:

答案 0 :(得分:1)

您未定位DOM元素,因为该元素在文档中当时不存在。

该脚本需要在文档之后加载,将脚本放在body标签之后,这应该可以解决问题。

:)