为什么我会得到这个“null or not an object”? IE 出错?
我有一个按钮(
)。我希望加载时自动单击此按钮。因此,我添加了 JavaScript 来实现这一点:
<script language="javascript" type="text/javascript">
var myButton = document.getElementById('myButton');
myButton.click();
</script>
我收到的错误是“Microsoft JScript 运行时错误:'myButton' 为 null 或不是对象”
该按钮所在的页面后面有一个 MasterPage。这就是发生这种情况的原因吗?
I have a button (<asp:button id="MyButton" runat="server" />
). I want this button to be automatically clicked onload. So I added JavaScript for that to happen:
<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"
The page this button is on has a MasterPage in the back. Would that be the reason this is happenening?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
document.getElementById()
区分大小写。尝试document.getElemenById('MyButton')
。document.getElementById()
is case sensitive. Trydocument.getElemenById('MyButton')
.您想要写入客户端 ID 而不是服务器 ID。
我明白了,这段代码将在按钮加载到客户端之前运行,因此您需要将此脚本块放在按钮之后,或者如果您使用 jQuery 或类似的东西,它们将提供文档就绪功能。文档完全加载后,将运行以下代码。
您需要设置一些属性来实现您的要求。
将
UseSubmitBehavior
设置为false
意味着该按钮不会回发到服务器。OnClientClick
属性顾名思义。单击时,客户端将执行指定的 JavaScript 代码。You want to write the client ID rather than the server ID.
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. The following code will be run once the document has loaded completely.
You'll want to set a few properties to implement your requirements.
Setting the
UseSubmitBehavior
tofalse
means that the button will not post back to the server. TheOnClientClick
property is as literal as it sounds. When clicked the client will execute the JavaScript code specified.客户端渲染的 HTML 中按钮的 Id 是什么? ASP .NET 倾向于自动生成更长的客户端 ID 供自己使用。您可以做的一件事是设置按钮的
name
属性并在 JavaScript 中引用该属性。对您也有潜在用处的是一种使用 jQuery 查找 .NET 客户端 ID 的方法< /a>.
What is the Id of the button in the rendered HTML on the client side? ASP .NET has a tendency to have longer auto-generated client-side IDs for its own use. One thing you could do is set the
name
attribute of the button and reference that in the JavaScript.Also of potential use to you is a method of finding a .NET client ID using jQuery.
该按钮在呈现给客户端时可能有另一个 ID。尝试为按钮设置
ClientIDMode="Static"
。那么 id 将与服务器端的 id 相同。The button probably has another ID when it's rendered to the client. Try to set
ClientIDMode="Static"
for the button. Then the id will be then same as on the server side.将 id="MyButton" 更改为 id="myButton" 大小写在 javascript/DOM 中很重要
Change the id="MyButton" to id="myButton" case matters in javascript/DOM
该页面可能尚未完全加载,因此当执行此代码时,“myButton”可能尚不可用。您可以创建一个在 body load: 上调用此函数的函数:
或者将脚本放在 body 标记的末尾:
同时确保大小写相同。我的按钮 != 我的按钮
The page may not have loaded fully so when this code is executed 'myButton' may not be available yet. You can either make a function that calls this on body load:
or put your script at the end of the body tag:
Also make sure the case is the same. MyButton != myButton
将脚本更改为:
并将此脚本放在页面末尾
之前。
Change your script to :
And put this script at the end of page just before
</body>
.