javascript - TypeError:无法读取 null 的属性“getAttribute”
问题描述
尝试从 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>
解决方案
您没有针对 DOM 元素,因为它在文档中当时不存在。
脚本需要在文档之后加载,将脚本放在 body 标记之后,这应该可以解决问题。
:)
推荐阅读
- azure - 在 ADO 中部署后检测 Azure 插槽交换是否失败?
- javascript - 突出显示并复制没有分隔符的数字
- laravel - 如何告诉 Laravel Eloquent 在同一个模型中不止一对多?
- android - 跟踪归因活动的 Android Apk 下载源(不是 Google Play)
- excel - 如何将具有白色填充的单元格更改为无填充?
- mysql - MySql 自定义选择与连接表
- oauth - 如何使用 Keycloak 将查询参数转发给外部身份提供者?
- uwp - 在 4K 显示器上运行的 UWP 应用程序因 E_RUNTIME_SETVALUE 错误而崩溃
- django - Django Rest 框架 - 过滤 URL - get() 得到一个意外的关键字参数“用户名”
- html - 对 Bootstrap css 网格、flex 和行布局感到困惑