简体   繁体   English

JS li标签onclick在IE8上不起作用

[英]JS li tag onclick not working on IE8

Am using Below JS,but li onclick is not working on IE8 browser. 我正在使用Under JS,但是li onclick在IE8浏览器上无法使用。

jsfiddle link : jsfiddle链接:

http://jsfiddle.net/sudheera/DUZ3B/14/ http://jsfiddle.net/sudheera/DUZ3B/14/

HTML HTML

<div class="primaryNav fl">
<ul id="hd_vertical" class="productNav">

<li id="hd_test" class="flight">
<span class="navIcon flightIcon hd_test">Test</span>
<a class="hd_test" href="http://validator.w3.org/">Flights</a>
</li>

<li id="hd_test1" class="bus">
<span class="navIcon busIcon hd_test1">Test</span>
<a class="hd_test1" href="http://www.w3schools.com/">Buses</a>
</li>

</ul>
</div>

JS JS

var changeLocation = function(id) {
  var _url = document.getElementsByClassName(id)[1].getAttribute('href');
  location.href = _url;   
}

document.getElementById("hd_vertical").addEventListener("click",function(e) {
        if(e.target.nodeName == "LI") { 
            var _anchor = e.target.id;
            changeLocation(_anchor);
        } else if(e.target.nodeName == "SPAN") {
            var span = e.target;
            var li = span.parentNode;
            var _anchor = li.id;   
            changeLocation(_anchor);
    }
});

please suggest 请建议

IE8 and earlier don't have addEventListener , but they do have its non-standard predecessor, attachEvent . IE8和更早的版本没有addEventListener ,但是它们确实有其非标准的前身attachEvent They're not quite the same. 他们是不太一样的。

Here's a "hook this event" function that uses what's available: 这是使用可用功能的“挂钩此事件”功能:

var hookEvent = (function() {
    var div;

    // The function we use on standard-compliant browsers
    function standardHookEvent(element, eventName, handler) {
        element.addEventListener(eventName, handler, false);
        return element;
    }

    // The function we use on browsers with the previous Microsoft-specific mechanism
    function oldIEHookEvent(element, eventName, handler) {
        element.attachEvent("on" + eventName, function(e) {
            e = e || window.event;
            e.preventDefault = oldIEPreventDefault;
            e.stopPropagation = oldIEStopPropagation;
            handler.call(element, e);
        });
        return element;
    }

    // Polyfill for preventDefault on old IE
    function oldIEPreventDefault() {
        this.returnValue = false;
    }

    // Polyfill for stopPropagation on old IE
    function oldIEStopPropagation() {
        this.cancelBubble = true;
    }

    // Return the appropriate function; we don't rely on document.body
    // here just in case someone wants to use this within the head
    div = document.createElement('div');
    if (div.addEventListener) {
        div = undefined;
        return standardHookEvent;
    }
    if (div.attachEvent) {
        div = undefined;
        return oldIEHookEvent;
    }
    throw "Neither modern event mechanism (addEventListener nor attachEvent) is supported by this browser.";
})();

Then you'd use it like this in your example: 然后,您将在示例中使用它:

hookEvent(document.getElementById("hd_vertical"), "click", function(e) {
    // ...
});

Note how it provides the missing preventDefault and stopPropagation functions on the event object on browsers using attachEvent and ensures that this within the handler call is what it would be if you were using addEventListener . 注意它如何提供失踪preventDefaultstopPropagation上使用浏览器的事件对象功能attachEvent并确保this处理程序调用中是如果你使用它会是什么addEventListener

There are various aspects of event normalization that the above does not do: 有活动正常化的各个方面,上面没有做:

  1. It doesn't guarantee the order in which the handlers will run ( attachEvent does them in the opposite order to addEventListener ) 它不能保证处理程序的运行顺序( attachEvent以与addEventListener相反的顺序进行处理)

  2. It doesn't handle issues around e.which vs. e.keyCode and such 它不处理周围的问题e.whiche.keyCode和这样的

...and of course, I haven't provided a detach event function. ...当然,我还没有提供分离事件功能。 :-) For things like that, look at using a library like jQuery , YUI , Closure , or any of several others . :-)对于类似的事情,请看使用jQueryYUIClosure其他几种库


Side note: As adeneo pointed out in a comment on the question, IE8 also doesn't support getElementsByClassName . 旁注:正如adeneo在对问题的评论中指出的那样,IE8也不支持getElementsByClassName But it does support querySelectorAll and querySelector , so change: 但它确实支持querySelectorAllquerySelector ,因此请更改:

var _url = document.getElementsByClassName(id)[1].getAttribute('href');

to

var _url = document.querySelectorAll("." + id)[1].getAttribute('href');

Note that that will try to get the second element matching the selector. 请注意,这将尝试获取与选择器匹配的第二个元素。 [1] is the second entry in the list, not the first, which would be [0] . [1]是列表中的第二个条目,而不是第一个,应该是[0] If you meant the first, you can use querySelector , which returns just the first match: 如果您是第一个,则可以使用querySelector ,它仅返回第一个匹配项:

var _url = document.querySelector("." + id).getAttribute('href');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM