Home > Web Front-end > JS Tutorial > body text

Introduction to the difference between dom0 level events and dom2 level events in JS_javascript skills

WBOY
Release: 2016-05-16 15:01:57
Original
1543 people have browsed it

dom level 0 event

<a href="#" id="hash" onclick="fn();fn();">
<button type="button">返回上面进行开通</button>
</a> 
var btn=$('#hash').get();
btn.onclick=function(){
alert('');
};
btn.onclick=function(){
alert('');
}; 
Copy after login

Writing onclick in the tag like above is a dom0 level event, and fn and fn1 are executed in sequence; the second way to get an element and bind an onclick event is also at dom0 level, and the second one will overwrite the first onclick. Overriding the onclick within the row will only pop up 222.

dom2 level event

$('#hash').click(function(){
alert('jq的dom2级点击第一次')
});
$('#hash').click(function(){
alert('jq的dom2级点击第二次')
});
btn.addEventListener('click',function(){
alert('原生dom2级第一次click')
},false);
btn.addEventListener('click',function(){
alert('原生dom2级第二次click')
},false) 
Copy after login

The above bindings are all DOM2-level event bindings. The first two are jq binding methods, and the latter two are native js binding methods. They will not be overwritten and will execute jq binding methods and native ones in sequence. The binding method, this is to go elsewhere at dom0 level;

dom0 and dom2 coexist

<a href="#" id="hash" onclick="fn();fn1();">
<button type="button">返回上面进行开通</button>
</a>
<script type="text/javascript">
function fn(){
alert('ade');
}
function fn1(){
alert('ade111');
}
var btn=$('#hash').get(0);
btn.onclick=function(){
alert('111');
};
$('#hash').click(function(){
alert('jq的dom2级点击第一次')
});
btn.addEventListener('click',function(){
alert('原生dom2级第一次click')
},false);
</script> 
Copy after login

The above example has two dom0 level and two dom3 level binding events. The dom0 level written in js will override the fn and fn1 methods in the line, but dom0 in js can coexist with dom2, and the result is 111. The first click of jq’s dom2 level is the first time the native dom2 level is clicked;

The above content is the editor’s introduction to the differences between dom0-level events and dom2-level events in JS. I hope it will be helpful to you. At the same time, I am very grateful for your support of the Script House website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template