gpt4 book ai didi

javascript - getElementById 引用 Null(从 Javascript 调用)

转载 作者:行者123 更新时间:2023-11-28 01:03:21 24 4
gpt4 key购买 nike

我在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>

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

附注iframe 是从 contentplaceholder 调用的。

最佳答案

简单添加

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

而不是

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

关于javascript - getElementById 引用 Null(从 Javascript 调用),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25360605/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com