为什么我会得到这个“null or not an object”? IE 出错?

发布于 2024-10-07 15:25:22 字数 421 浏览 11 评论 0原文

我有一个按钮()。我希望加载时自动单击此按钮。因此,我添加了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

独留℉清风醉 2024-10-14 15:25:22

document.getElementById() 区分大小写。尝试 document.getElemenById('MyButton')

document.getElementById() is case sensitive. Try document.getElemenById('MyButton').

绝影如岚 2024-10-14 15:25:22

您想要写入客户端 ID 而不是服务器 ID。

<script language="javascript" type="text/javascript">
      var myButton = document.getElementById('<%=MyButton.ClientID%>');
      myButton.click();
</script> 

我明白了,这段代码将在按钮加载到客户端之前运行,因此您需要将此脚本块放在按钮之后,或者如果您使用 jQuery 或类似的东西,它们将提供文档就绪功能。文档完全加载后,将运行以下代码。

$(document).ready(function () {
    var myButton = $('#<%=MyButton.ClientID%>');
    myButton.click();
});

您需要设置一些属性来实现您的要求。

<asp:button 
    id="MyButton" 
    runat="server" 
    UseSubmitBehavior="false" 
    OnClientClick="return changeBackgroundColor();" />

UseSubmitBehavior 设置为 false 意味着该按钮不会回发到服务器。 OnClientClick 属性顾名思义。单击时,客户端将执行指定的 JavaScript 代码。

You want to write the client ID rather than the server 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. 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. The OnClientClick property is as literal as it sounds. When clicked the client will execute the JavaScript code specified.

迷迭香的记忆 2024-10-14 15:25:22

客户端渲染的 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.

赠我空喜 2024-10-14 15:25:22

该按钮在呈现给客户端时可能有另一个 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.

御弟哥哥 2024-10-14 15:25:22

将 id="MyButton" 更改为 id="myButton" 大小写在 javascript/DOM 中很重要

Change the id="MyButton" to id="myButton" case matters in javascript/DOM

荆棘i 2024-10-14 15:25:22

该页面可能尚未完全加载,因此当执行此代码时,“myButton”可能尚不可用。您可以创建一个在 body load: 上调用此函数的函数:

<body onload="functionForButton()">

或者将脚本放在 body 标记的末尾:

<script language="javascript" type="text/javascript">
      var myButton = document.getElementById('myButton');
      myButton.click();
</script> 
</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:

<body onload="functionForButton()">

or put your script at the end of the body tag:

<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

謸气贵蔟 2024-10-14 15:25:22

将脚本更改为:

<script language="javascript" type="text/javascript">
    var myButton = document.getElementById('<%= myButton.ClientID %>');
    myButton.click();
</script>

并将此脚本放在页面末尾 之前。

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>.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文