Javascript converting addEventListener to attachEvent
I have the following code calling a Javascript function in a nice shiny standards complient manner :). Please note that I must send elementsList[i]
to the function (the this keyword will not be adaquate) as the event listerner is being attached to it's nephew (for want of a better term)
This rather mangled code will effectively find the control element for the dynamic behaviour of the current node in elementsList[i] and add a click event listener to it. When fired it passes the clicked node to the toggle function.
elementsList[i].previousSibling.lastChild.addEventListener
("click", (function(el){return开发者_StackOverflow function(){toggle(el)};})(elementsList[i]),false);
Thing is it doesn't work at all with IE8 and below, and in spite of spending most of the morning trying to find a work around I just can't get it to play ball. If someone knows how to translate this into IE crapo code, I'd be grateful to see it.
Have you tried
elementsList[i].previousSibling.lastChild.attachEvent
("onclick", (function(el){return function(){toggle(el)};})(elementsList[i]),false);
2 potential suggestions.
1.) Include jQuery in your project and use their x-browser event bindings (or mootools or some other lib)
2.) Not so-recommended, roll your own x-browser event bindings
精彩评论