IE8 "Onclick" Event Listener Issues with
When using the provided JavaScript code in IE8, the "onclick" event handler for
Solution:
To resolve this issue, a workaround involving the non-standard predecessor to "addEventListener" in IE8, namely "attachEvent," can be employed. Here's a modified version of the JavaScript code that uses this method:
hookEvent(document.getElementById("hd_vertical"), "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); } });
Here's how this code works:
Note: IE8 also lacks support for "getElementsByClassName." Consider using "querySelector" or "querySelectorAll" instead:
var _url = document.querySelector("." + id).getAttribute('href');
By implementing these changes, "onclick" event handlers for
The above is the detailed content of Why Do My `` onclick Event Handlers Fail in IE8, and How Can I Fix Them?. For more information, please follow other related articles on the PHP Chinese website!