简体   繁体   English

为什么我在IE中收到此“空或非对象”错误?

[英]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.

相关问题 为什么我在IE8中收到“对象预期”错误? - Why am I getting an “object expected” error in IE8? 为什么我收到此错误消息:(空属性) - Why am i getting this error message am getting: (property of null) Facebook all.js-为什么在Safari中出现此错误:TypeError:“ null”不是对象 - Facebook all.js - Why am I getting this error in Safari: TypeError: 'null' is not an object null 不是 object(评估 'document.getElementById('logout-form').submit') - 为什么会出现此错误? - null is not an object (evaluating 'document.getElementById('logout-form').submit') - Why am I getting this error? 为什么会出现“无法读取null的属性样式”错误? - Why I am getting “cannot read property style of null” error? 无法将属性“ innerHTML”设置为null,为什么会出现此错误 - Cannot set property 'innerHTML' of null why am I getting this error 为什么我会收到“输出为空”(glmatrix)错误? Webgl - Why am i getting “out is null” (glmatrix) error? Webgl 在带有数组的对象上使用它时,为什么会出现$ parse错误? - Why I am getting $parse error when use it on object with array? 为什么我变得未定义不是Vue JS中的对象错误 - Why am I getting undefined is not an object error in vue js 为什么我的对象方法出现“不是函数错误”? - why am I getting “is not a function error” for my object method?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM