Javascript 锚标记中的 onclick 事件在 IE 中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3653722/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 05:36:35  来源:igfitidea点击:

onclick event in anchor tag not working in IE

javascriptinternet-exploreronclickanchor

提问by Mark

The following works fine in Firefox and Chrome, but IE 8 will not call the submit() method when the anchor link is clicked.

以下在 Firefox 和 Chrome 中工作正常,但 IE 8 在单击锚链接时不会调用 submit() 方法。

<a href="javascript:void(0);" onclick="submit();">Sign In</a>

The submit method is defined on the same page as follows:

submit 方法在同一页面上定义如下:

<head>
<script type="text/javascript">

function submit()
{
// other code                                              
document.forms[0].submit();
}  

</script>
</head>

回答by T.J. Crowder

Can you provide a bit more context? Such as where and how the submitfunction is defined? With just the above, it should work -- except:

你能提供更多的背景吗?比如submit函数在哪里以及如何定义?仅凭上述内容,它应该可以工作——除了:

You probably also want to return false;in there though, to cancel the default action. E.g.:

return false;不过,您可能还想在那里取消默认操作。例如:

<a href="javascript:void(0);" onclick="submit();return false;">Sign In</a>

It may be that the default action is happening immediately after the submitand interfering with it.

可能是默认操作在 之后立即发生submit并干扰它。

Edit: Just for fits and giggles, try using a different name for your submitfunction. IE has namespacing issues. If you have anything with the idor nameattribute set to "submit", for instance, that could be an issue...

编辑:只是为了适合和咯咯笑,尝试为您的submit功能使用不同的名称。IE 有命名空间问题。例如,如果您将idorname属性设置为“提交”,这可能是一个问题......

回答by Neil King

For others who have come across this problem more recently, for me it was due to IE 11 using compatibility modeon the site. It saw I was on the same domain, assumed the site was an intranet site and automatically switched to compatibility mode which broke some of the Javascript (and caused some other unexpected display issues).

对于最近遇到此问题的其他人,对我而言,这是由于 IE 11在站点上使用了兼容模式。它看到我在同一个域中,假设该站点是 Intranet 站点并自动切换到兼容模式,这破坏了一些 Javascript(并导致了一些其他意外的显示问题)。

You can turn off this 'feature' by clicking on the settings cog and choosing "Compatibility View Settings". There is a tick box that was set called "Display intranet sites in Compatibility View".

您可以通过单击设置齿轮并选择“兼容性视图设置”来关闭此“功能”。有一个勾选框被设置为“在兼容性视图中显示 Intranet 站点”。

回答by irishbuzz

Try using

尝试使用

<a onclick="submit();">Sign In</a>

or

或者

<a href="javascript:submit();">Sign In</a>

回答by lzlstyle

If you need to input the value to the javascript function, make sure that uses the single quotation instead of the double quotation.

如果需要将值输入到 javascript 函数中,请确保使用单引号而不是双引号。

For example:

例如:

<a href="#" onclick="your_function('input');">Sign In</a>