为什么我在 Firefox 中遇到 Asp:LinkButton 的 .Click() 事件错误?
我试图在单击按钮时触发 click() 事件。
目前,这在 IE 中适用于我,但是我在 Firefox 中收到以下错误:
“link.click 不是一个函数”
我一直在谷歌上搜索有关此问题的信息,发现并非所有版本的 Firefox 都支持 .click 事件。
有人能够提供替代方案吗?代码如下:
<asp:LinkButton ID="ButtonNext" runat="server" CssClass="RemoveLinkStyle" TabIndex="1"></asp:LinkButton>
<table id="tblButtonNext" runat="server" cellpadding="0" cellspacing="0" class="pwbtn" onclick="ButtonClick(ButtonNext);" TabIndex="1" onmouseout="this.className='pwbtn';" onmouseover="this.className='pwbtnh';">
<tr>
<td class="a1"></td>
<td class="a2">
<%= this.resourceManager.GetString("nextstep") %>
</td>
<td class="a3"></td>
<td class="spacer"></td>
</tr>
</table>
function ExecuteLink(linkID)
{
var link = document.getElementById(linkID);
link.click();
}
function ButtonClick(linkID)
{
PreNavigationScript();
CallShowBlocker();
ExecuteLink(linkID);
}
I am attempting to fire the click() event when a button is clicked.
This currently works for me in IE however I receive the following error in firefox:
"link.click is not a function"
I have been trawling google regarding this and found out the .click event is not supported in all versions of firefox.
Is anyone able to provide an alternative? Code below:
<asp:LinkButton ID="ButtonNext" runat="server" CssClass="RemoveLinkStyle" TabIndex="1"></asp:LinkButton>
<table id="tblButtonNext" runat="server" cellpadding="0" cellspacing="0" class="pwbtn" onclick="ButtonClick(ButtonNext);" TabIndex="1" onmouseout="this.className='pwbtn';" onmouseover="this.className='pwbtnh';">
<tr>
<td class="a1"></td>
<td class="a2">
<%= this.resourceManager.GetString("nextstep") %>
</td>
<td class="a3"></td>
<td class="spacer"></td>
</tr>
</table>
function ExecuteLink(linkID)
{
var link = document.getElementById(linkID);
link.click();
}
function ButtonClick(linkID)
{
PreNavigationScript();
CallShowBlocker();
ExecuteLink(linkID);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我会为此使用 jQuery 。 jQuery 公开的
click
事件被设计为跨浏览器兼容,您可能会发现它的工作方式比您当前使用的更加一致。然后你的方法将变成:(
你可能想检查 $(linkID) 是否也返回一个元素)
I'd use jQuery for this. The
click
event exposed by jQuery is designed to be cross browser compatible and you'll likely find it works more consistently than what you're currently using.Your method will then become:
(You probably want to check whether $(linkID) returns an element also)
设置
CausesValidation="False"
将解决问题Setting
CausesValidation="False"
will solve the problem如果您不使用 jQuery,那么您的替代方案是 Firefox 的相当 hacky 鼠标事件,例如
按照 mozilla.dev.tech.dom
但是,另一种选择是调用处理按钮单击事件的函数。查看源代码并检查它是什么,它将类似于 __doPostback('ButtonNext',...); 并在您的 ExecuteLink() 函数中调用它。
If you are not using jQuery then your alternative is pretty hacky mouse event for firefox, e.g.
as per mozilla.dev.tech.dom
However, another alternative is to call the function handling your button click event. View Source and check what it is, it'll be something like
__doPostback('ButtonNext',...);
and call it in your ExecuteLink() funct.我也尝试了Jquery的click()方法,但该事件从未被触发。
您可以直接访问 Linkbutton 生成的 javascript 方法:
ASP-Linkbutton 被呈现到最终 HTML 代码中的输入字段和锚点。第一个参数是该输入字段的(客户端)名称。
I tried the click()-Method of Jquery too, but the event was never triggered.
You can access the generated javascript Method of the Linkbutton directly:
The ASP-Linkbutton is rendered to a input field and an anchor in the final HTML-Code. The first parameter is the (client-)name of this inputfield.
只需将此代码片段添加到您的 .master 页或任何其他适当的页面中的
标记之前:
将“aspnetForm”替换为您自己的代码片段。
Just add this code snippet in your .master page or any other appropiate page just before the
</body>
tag:Replace 'aspnetForm' with your own.