Onclick event in anchor tag not working in IE10

隐身守侯 提交于 2019-12-11 20:33:17

问题


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

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

http://alt.thedominion.ca/TheBrokerAdvantage/LocateABroker.aspx

Any help is greatly appreciated!


回答1:


The problem is with your server side code, not the browser or JavaScript.

If you check the JavaScript console in IE10 you will see the following error when clicking the link:

SCRIPT5009: 'ValidatorValidate' is undefined

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.

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 defined is not included, meaning the server never put the line <script src="..."> with that URL as part of the output to the browser.

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:

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

You will have to upgrade your Sitefinity if you want IE10 support.




回答2:


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

<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 option:

a{
    display: block;
    width: 100px;
    height: 20px;
}

Working CSS Example



来源:https://stackoverflow.com/questions/16149131/onclick-event-in-anchor-tag-not-working-in-ie10

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!