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

别说谁变了你拦得住时间么 提交于 2019-12-18 02:58:08

问题


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.


回答1:


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

<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....




回答2:


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.



来源:https://stackoverflow.com/questions/5642659/what-is-the-difference-between-dom-level-0-events-vs-dom-level-2-events

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!