简体   繁体   中英

Bubbling an event triggered by disabled element

The question is: should the disabled element produce an event that will be triggered on its parent(s)?

<div id="test">
  <button disabled="disabled">Click me</button>
</div>
<script type="text/javascript">

document.getElementById("test").onclick = function() {
  alert("Clicked!");
};

</script>

All browsers except IE prevent the event from being fired, but IE doesn't. Is this behavior documented or standardized? Which of browsers process the code above correctly?

As per http://www.quirksmode.org/js/events_advanced.html I highly recommend to use event delegation instead of .onclick() binding. Example:

var element = document.getElementById('test'),
    doSomething = function () {
        alert("Clicked!");
    };
if (element.addEventListener) {
    element.addEventListener('click', doSomething, false);
} else if (element.attachEvent) {
    elem.attachEvent('onclick', doSomething);
}

:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM