[removed] How to simulate change event in internet explorer (delegation)

后端 未结 2 1283
悲哀的现实
悲哀的现实 2020-11-29 12:13

UPDATE: (recap, fiddle and bounty)

This question hasn\'t been getting too much attention, so I\'m going to spend some rep on it. I know I t

2条回答
  •  甜味超标
    2020-11-29 12:43

    Well, I had another crack at it, and I've come up with a fairly decent approach (at work it did the trick - I've tried to replicate the code I wrote but after a few beers it might contain some errors but the spirit remains the same)

    window.attachEvent('onload',function ieLoad()
    {
        var mainDiv = document.getElementById('main');//main div, from here events will be delegated
        var checks = mainDiv.getElementsByTagName('input');//node list of all inputs
        var checkStates = {};
        for (var i=0;i

    I've found out that the focusin events fire twice in some cases for radio's and checkboxes. Using an object that holds the actual checked states of all checkboxes is less expensive than individual handlers, and it allows me to only delegate the event after the value of the element has changed.

    The changeDelegator function is only called when needed, but the anon function that I posted here still gets called Waaaay more than I wanted it, but this approach still outperforms the individual handlers-take.

    I left out the selects, but I got them working, too (similar take, in the full version of my code the closure has 2 objects, and I made it, so I can flag an id, fire the blur event when needed, and the client is redirected).
    At the end of the run, even though I've learned some new tricks, the main thing I take away from this exercise is an even more profound hatred of that ghastly, gritty golem of a thing called IE... But if anybody else might ever want to delegate change events in IE, know that it is (almost) possible

提交回复
热议问题