getElementById引用Null(从Javascript调用)

时间:2014-08-18 09:56:39

标签: javascript html asp.net iframe

我有用 iframe 编写的 Javascript 代码,即在父窗口的contentplaceholder中修改 hiddenfield 的值,还有触发按钮的点击事件,并设置iframe的可见性。它们都被错误引用。

iframe中的Javascript:

 <script type="text/javascript">
      function ChangeView(obj) {

          parent.document.getElementById("HiddenField1").value = $(obj).attr('id');
          parent.document.getElementById("Button1").click();
          var iframe = parent.document.getElementById("newsFrame");
          iframe.style.display = "block";

      }

    </script>
</head>
<body>


 <form id="f">
    <nav>
      <ul id="nav">
        <li><a id="aHome"  href="#" onclick="ChangeView(this)" >Home</a></li>   
        <li><a href="#">About</a>
          <ul>
            <li><a id="aMessage" href="#" onclick="ChangeView(this)">Message</a></li>

          </ul>
        </li>
      </ul>
    </nav>
 </form>
</body>

父页:

<asp:Content ID="Content3"  ContentPlaceHolderID="ContentPlaceHolder2" runat="server">
   <html xmlns="http://www.w3.org/1999/xhtml" > 

<head>
    <title> </title>
<script runat="server">

    Sub ChangeView()
        Dim t As HiddenField
        ' Dim mc As Control = FindControl("Label1")
        t = FindControl("HiddenField1")
        Dim m As MultiView
        m = FindControl("Multiview1")

        If (t.Value = "aHome") Then
            m.ActiveViewIndex = 0

        End If
        If (t.Value = "aMessage") Then
            m.ActiveViewIndex = 1
        End If

    End Sub

   </script>

</head>

<body>
   <form id="form1" runat="server">

   <asp:Button ID="Button1" runat="server"  Visible="True" OnClick="ChangeView"  />

       <asp:ScriptManager ID="SM1" runat="server" />

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
        <asp:HiddenField ID="HiddenField2"  runat="server"  />
         <asp:MultiView ID="MultiView2" runat="server" ActiveViewIndex="0">
            <asp:View ID="View8" runat="server">
                This is HOME 
            </asp:View>
            <asp:View ID="View9" runat="server">
                This is DEPARTMENT MESSAGE 
            </asp:View>

        </asp:MultiView>
        <br /><br />


        </ContentTemplate>
    </asp:UpdatePanel>
   </form>

</body>
</html>
                <iframe src="dept-menu/index.html" width="100%" height="650px" scrolling="no" frameborder="0"></iframe>

                    <iframe id="newsFrame" src="dept-news/news.html" width="100%" height="300px" scrolling="no" frameborder="0"></iframe>



</asp:Content>

HTML

 <input type="submit" name="ctl00$ContentPlaceHolder2$Button1" value="" id="Button1" />


       <script type="text/javascript">
//<![CDATA[
Sys.WebForms.PageRequestManager._initialize('ctl00$ContentPlaceHolder2$SM1', 'form1', ['tctl00$ContentPlaceHolder2$UpdatePanel1','ContentPlaceHolder2_UpdatePanel1'], [], [], 90, 'ctl00');
//]]>
</script>



<div id="ContentPlaceHolder2_UpdatePanel1">

        <input type="hidden" name="ctl00$ContentPlaceHolder2$HiddenField1" id="HiddenField1" />

                This is HOME 

        <br /><br />



</div>

在运行时,错误表明他们无法设置空引用的值。我做错了什么?提前谢谢。

P.S。 iframe是从contentplaceholder调用的。

1 个答案:

答案 0 :(得分:1)

简单地添加

      parent.document.getElementById("ContentPlaceHolder2_HiddenField1").value = $(obj).attr('id');

而不是

      parent.document.getElementById("HiddenField1").value = $(obj).attr('id');