JS li onclick 無法在IE8 上運行
問題:
問題:在提供的程式碼中,li元素的Internet Explorer中未觸發點選事件8.
解決方案:IE8不支援addEventListener。相反,請使用其非標準前身,attachEvent。
程式碼修改:var hookEvent = (function() { var div; function standardHookEvent(element, eventName, handler) { element.addEventListener(eventName, handler, false); return element; } 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; } function oldIEPreventDefault() { this.returnValue = false; } function oldIEStopPropagation() { this.cancelBubble = true; } 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."; })();
hookEvent(document.getElementById("hd_vertical"), "click", function(e) { // Your event handling code here });
接下來,使用這個函數來掛接事件監聽器:
var _url = document.querySelectorAll("." + id)[1].getAttribute('href');
以上是為什麼我的 JS `li` onclick 不能在 IE8 工作?的詳細內容。更多資訊請關注PHP中文網其他相關文章!