javascript 锚标记中的 Onclick 事件在 IE10 中不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16149131/
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
Onclick event in anchor tag not working in IE10
提问by Yosief Kesete
I've an anchor in an Asp.Net user control. I've wired some code to the onclick event. For some reason in Internet Explorer 10, the event is not firing. The markup is structured as
我在 Asp.Net 用户控件中有一个锚点。我已经将一些代码连接到 onclick 事件。由于某些原因,在 Internet Explorer 10 中,事件没有触发。标记的结构如下
<a href="#" id="myAnchor" onclick="myFunction();return false;"></a>
where myFunction is a simple JavaScript function. For those who want to see the page in action, here is the link:
其中 myFunction 是一个简单的 JavaScript 函数。对于那些想要查看页面运行情况的人,这里是链接:
http://alt.thedominion.ca/TheBrokerAdvantage/LocateABroker.aspx
http://alt.thedominion.ca/TheBrokerAdvantage/LocateABroker.aspx
Any help is greatly appreciated!
任何帮助是极大的赞赏!
回答by What have you tried
The below code works on every major browser (I just tested it on IE10, IE9, Chrome 26.0.1410.64, and the latest firefox release).
下面的代码适用于所有主流浏览器(我刚刚在 IE10、IE9、Chrome 26.0.1410.64 和最新的 firefox 版本上进行了测试)。
<a href="#" id="myAnchor" onclick="myFunction();return false;">click me</a>
<script type="text/javascript">
function myFunction(){
alert("Anchor was clicked");
}
</script>
In other words, your syntax is fine, but the anchor tag simply will not appear without inner text unless you use CSS to force a width and height.
换句话说,你的语法很好,但锚标签不会在没有内部文本的情况下出现,除非你使用 CSS 来强制设置宽度和高度。
CSS option:
CSS 选项:
a{
display: block;
width: 100px;
height: 20px;
}
Working CSS Example
工作 CSS 示例
回答by Shadow Wizard is Ear For You
The problem is with your server side code, not the browser or JavaScript.
问题在于您的服务器端代码,而不是浏览器或 JavaScript。
If you check the JavaScript console in IE10 you will see the following error when clicking the link:
如果您在 IE10 中检查 JavaScript 控制台,您将在单击链接时看到以下错误:
SCRIPT5009: 'ValidatorValidate' is undefined
SCRIPT5009: 'ValidatorValidate' 未定义
Which points to the second line in the function:
指向函数中的第二行:
function doSearch() {
var regExValidate = document.getElementById("ctl00_BrokerSearchMiddle_ctl00_ValidPostalCode");
ValidatorValidate(regExValidate);
var postalCode = $find("ctl00_BrokerSearchMiddle_ctl00_PostalCode").get_value();
if (regExValidate.isvalid && postalCode.indexOf("e.g") == -1) {
document.location.href = "?postalCode=" + postalCode;
}
}
This means the onclick
is working just fine, you simply have JS error.
这意味着onclick
它工作得很好,你只是有 JS 错误。
Now the question is why ValidatorValidate
exists in other browsers (even IE9) but not in IE10. Well, in IE10 the script where it's being definedis not included, meaning the server never put the line <script src="...">
with that URL as part of the output to the browser.
现在的问题是为什么ValidatorValidate
在其他浏览器(甚至 IE9)中存在,而在 IE10 中不存在。好吧,在 IE10 中,定义它的脚本不包括在内,这意味着服务器永远不会将<script src="...">
带有该 URL的行作为输出到浏览器的一部分。
I can only guess that the server side code is checking the browser version and according to that include certain scripts. Check that code and get rid of such things as it's never a good idea.
我只能猜测服务器端代码正在检查浏览器版本,并据此包含某些脚本。检查该代码并摆脱这样的事情,因为这从来都不是一个好主意。
After some research I found what's going on. You are using Sitefinity version 3.7 to build your site (according to this question of yours) and as officially stated here:
经过一番研究,我发现了发生了什么。您正在使用 Sitefinity 3.7 版来构建您的网站(根据您的这个问题),并在此处正式说明:
I want to inform you that unfortunately Sitefinity 3.7 does not support Internet Explorer 10 and your Sitefinity 3.7 might not work properly with this browser version. Apologies for the inconvenience
我想通知您,很遗憾,Sitefinity 3.7 不支持 Internet Explorer 10,您的 Sitefinity 3.7 可能无法在此浏览器版本上正常运行。不便之处敬请原谅
You will have to upgrade your Sitefinity if you want IE10 support.
如果您想要 IE10 支持,则必须升级您的 Sitefinity。