[英]Why am I getting this “null or not an object” error in IE?
I have a button ( <asp:button id="MyButton" runat="server" />
). 我有一个按钮(
<asp:button id="MyButton" runat="server" />
)。 I want this button to be automatically clicked onload. 我希望此按钮在加载时自动单击。 So I added JavaScript for that to happen:
因此,我添加了JavaScript以实现这种情况:
<script language="javascript" type="text/javascript">
var myButton = document.getElementById('myButton');
myButton.click();
</script>
The error I'm getting is "Microsoft JScript runtime error: 'myButton' is null or not an object" 我收到的错误是“ Microsoft JScript运行时错误:'myButton'为null或不是对象”
The page this button is on has a MasterPage in the back. 此按钮位于的页面背面有一个MasterPage。 Would that be the reason this is happenening?
这是发生这种情况的原因吗?
You want to write the client ID rather than the server ID. 您要编写客户端ID而不是服务器ID。
<script language="javascript" type="text/javascript">
var myButton = document.getElementById('<%=MyButton.ClientID%>');
myButton.click();
</script>
I see, this code will run before the button is loaded on the client so you will need to place this script block after the button or if you are using jQuery or something similar they will offer document ready functionality. 我知道,这段代码将在按钮加载到客户端之前运行,因此您需要将此脚本块放置在按钮之后,或者如果您使用的是jQuery或类似的东西,它们将提供文档就绪功能。 The following code will be run once the document has loaded completely.
文档完全加载后,将运行以下代码。
$(document).ready(function () {
var myButton = $('#<%=MyButton.ClientID%>');
myButton.click();
});
You'll want to set a few properties to implement your requirements. 您将需要设置一些属性来实现您的需求。
<asp:button
id="MyButton"
runat="server"
UseSubmitBehavior="false"
OnClientClick="return changeBackgroundColor();" />
Setting the UseSubmitBehavior
to false
means that the button will not post back to the server. 将
UseSubmitBehavior
设置为false
意味着该按钮将不会回发到服务器。 The OnClientClick
property is as literal as it sounds. OnClientClick
属性与它听起来一样。 When clicked the client will execute the JavaScript code specified. 单击时,客户端将执行指定的JavaScript代码。
document.getElementById()
is case sensitive. document.getElementById()
区分大小写。 Try document.getElemenById('MyButton')
. 尝试
document.getElemenById('MyButton')
。
What is the Id of the button in the rendered HTML on the client side? 客户端呈现的HTML中的按钮ID是什么? ASP .NET has a tendency to have longer auto-generated client-side IDs for its own use.
ASP .NET倾向于拥有更长的自动生成的客户端ID供自己使用。 One thing you could do is set the
name
attribute of the button and reference that in the JavaScript. 您可以做的一件事是设置按钮的
name
属性,并在JavaScript中引用它。
Also of potential use to you is a method of finding a .NET client ID using jQuery . 还有一种可能的用途是使用jQuery查找.NET客户端ID的方法 。
The button probably has another ID when it's rendered to the client. 该按钮在呈现给客户端时可能具有另一个ID。 Try to set
ClientIDMode="Static"
for the button. 尝试为按钮设置
ClientIDMode="Static"
。 Then the id will be then same as on the server side. 然后,该ID将与服务器端的ID相同。
在javascript / DOM中将id =“ MyButton”更改为id =“ myButton”
Change your script to : 将脚本更改为:
<script language="javascript" type="text/javascript">
var myButton = document.getElementById('<%= myButton.ClientID %>');
myButton.click();
</script>
And put this script at the end of page just before </body>
. 并将此脚本放在
</body>
之前的页面末尾。
The page may not have loaded fully so when this code is executed 'myButton' may not be available yet. 该页面可能尚未完全加载,因此当执行此代码时,“ myButton”可能尚不可用。 You can either make a function that calls this on body load:
您可以使函数在身体负荷时调用此函数:
<body onload="functionForButton()">
or put your script at the end of the body tag: 或将您的脚本放在body标签的末尾:
<script language="javascript" type="text/javascript">
var myButton = document.getElementById('myButton');
myButton.click();
</script>
</body>
Also make sure the case is the same. 还要确保大小写相同。 MyButton != myButton
MyButton!= myButton
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.