简体   繁体   中英

What is the difference between DOM Level 0 events vs DOM Level 2 events?

What is the difference between DOM Level 0 events vs DOM Level 2 events? I ask because I was told that Firefox and IE call them in a different order and I had never heard those terms before.

DOM Level 0 events were based around the concept of using element attributes or named events on DOM elements, eg:

<input type="button" onclick="clickMe();" />

Or

input.onclick = function() { ... };

With DOM Level 2, we've now got a more standardised approach to managing events and subscriptions, with addEventListener , removeEventListener , etc.

You can read more here here

It wasn't until IE8 that Microsoft added support for the W3C standard for event management to their browser. Not sure in what order they are called....

In addition to the issue that previous answer mentioned completely and correctly, which concentrated on the type of using event handlers to call functions or to perform some other JavaScript(I mean using inline registration model and traditional registration model vs using addEventListener(...), removeEventListener(...), or dispatchEvent(...) ) and also to add additional information to this duplicated question , there is another great difference between DOM Level 0 vs DOM Level 2 event model.

Through DOM Level 2 event model, it is simply possible that a specific object(for example via: document.getElementById("elementId")), with a specific event(one of click or load, ...) can be registered any number of event-handler functions. For example:

<!DOCTYPE html>
<html>
    <body>
        <button id="btn">Test it</button>
        <script>
            document.getElementById("btn").addEventListener("click", function(){alert("first alert");});
            document.getElementById("btn").addEventListener("click", function(){alert("second alert");});
        </script>

    </body>
</html>

This was one of the problems in DOM Level 0, which is handled via other solutions .

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