문제

When a user clicks a button out of a series of buttons, I'd like the browser to be able to detect which buttons are toggled as soon as they've clicked it.

Here's an example: http://jsfiddle.net/Uc6LV/11/

  • Try clicking "One" (nothing happens)
  • Now click "Two", it will display "one" (the previous one that was clicked

I changed the event listener from click to mouseup but it still seems to fire the event too early.

Any ideas?

도움이 되었습니까?

해결책

In your event listener, I would listen for a "click" event and delay the actual calculation via setTimeout(). This works in your fiddle:

 container.children[i].addEventListener("click", //click is better than mouseup
            function(){
                //let's delay execution until button is toggled
                setTimeout(function() {
                    notice.innerHTML = toggled().toString(); }, 1);
        })
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top