亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

未捕獲的類型錯誤:無法將屬性“類型”設置為 null

未捕獲的類型錯誤:無法將屬性“類型”設置為 null

拉丁的傳說 2022-11-11 13:31:26
我正在使用 asp.net 中的母版頁,并在 chrome 瀏覽器檢查工具控制臺中收到以下錯誤。未捕獲的類型錯誤:無法在 HTMLInputElement.onclick 的 myFunction (StudentPassword.aspx:258) 處設置屬性“類型”為 null我想問題出在腳本標簽上。我應該把它放在哪里?在標簽內部還是在內容頁的底部還是在母版頁的底部?母版頁是:  <%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Student.master.cs" Inherits="Project_Placements.Student" %> <!DOCTYPE HTML> <HTML> <head runat="server"> </head> <body> <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">        </asp:ContentPlaceHolder>  <script type="text/javascript">          function myFunction() {           var checkBox = document.getElementById("myCheck");           var pwd = document.getElementById("password");           if (checkBox.checked == true) {               pwd.type = 'password';           } else {               pwd.type = 'text';           }       }   }   </script>       </body>   </html>內容頁代碼如下: <%@ Page Title="" Language="C#" MasterPageFile="~/Student.Master" AutoEventWireup="true"  CodeBehind="StudentPassword.aspx.cs" Inherits="Project_Placements.WebForm7" %> <asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">    <form id="form1" runat="server"> <asp:TextBox ID="password" runat="server"  TextMode="Password" ></asp:TextBox> <label for="myCheck">Show Password :</label>   <input type="checkbox" id="myCheck" onclick="myFunction()">  </form>  </asp:Content>
查看完整描述

2 回答

?
回首憶惘然

TA貢獻1847條經驗 獲得超11個贊

ASP.Net 將所有id從服務器端運行的父元素鏈接到id當前元素的所有元素,除非在服務器上被覆蓋。所以輸出id實際上看起來像ContentPlaceHolder1$Content3$form1$password.

您可以使用 ends-with 選擇器來省略它。

var pwd = document.querySelector("[id$=password]").

但是請注意,仍然選擇唯一的id,這也將是唯一的,使用 end-with。

或者,您可以使用數據屬性并選擇:

<asp:TextBox ID="password" runat="server"  TextMode="Password" data-id="password" />
var pwd = document.querySelector("[data-id=password]");

最后你可以使用類似的東西:

var pwd = document.getElementById('<%=password.ClientID %>');

然而我從來沒有真正喜歡過它,它要求腳本是內聯的。



查看完整回答
反對 回復 2022-11-11
?
慕的地8271018

TA貢獻1796條經驗 獲得超4個贊

您收到錯誤是因為在完全渲染 DOM 之前加載了腳本。嘗試將腳本的內容放在 document.ready 中,這應該可以解決問題。



查看完整回答
反對 回復 2022-11-11
  • 2 回答
  • 0 關注
  • 147 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號