【问题标题】:javascript Cannot read property 'style' of nulljavascript 无法读取 null 的属性“样式”
【发布时间】:2015-05-13 05:15:36
【问题描述】:
 function showNotes() {

     var ThemeValues = "<%=str3%>"
     if (ThemeValues.value == "MyCity-Social") {

         document.getElementById("TextBox1").style.visibility = "visible";
         document.getElementById("TextBox2").style.visibility = "visible";
         $.blockUI({
             message: $('#divNotes'),
             css: {}
         });
     } else {

         //ERROR
         document.getElementById("TextBox1").style.display = "none";
         document.getElementById("TextBox2").style.display = "none";

         $.blockUI({
             message: $('#divNotes'),
             css: {}
         });

     }
 }

函数在顶部声明并在点击时调用,但错误高于描述,并且 var ThemeValues 不显示值

【问题讨论】:

  • 这意味着 Javascript 没有获取您的文本框或将其设为空。 document.getElementById("TextBox1") 为空。
  • 同时显示您的 html 代码
  • 你的 HTML 中真的有 textbox1 吗?

标签: javascript c# jquery asp.net vb.net


【解决方案1】:

在您的文本框控件上添加ClientIDMode="Static"

<asp:TextBox ID="TextBox1" runat="server" Width="385px" Height="50px" TextMode="MultiLine" ClientIDMode="Static"></asp:TextBox>

然后你上面的代码就可以工作了。

对于那里的ThemeValues 变量,你不会在那里得到任何控制值,因为你只在那里为变量分配了一个字符串。

改用这个

var ThemeValues = document.document.getElementById("<%=str3%>");

【讨论】:

    【解决方案2】:

    这样使用

    document.getElementById('<%= TextBox1.ClientID%>').style.display = "none"
    

    而不是

    document.getElementById("TextBox1").style.display = "none";
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-05
      • 2021-10-31
      • 1970-01-01
      相关资源
      最近更新 更多