Home  >  Article  >  Web Front-end  >  return false, some test code for blocking the default action of the event_javascript skills

return false, some test code for blocking the default action of the event_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:16:141123browse

First, there is a
We bind the following events to it

Copy code The code is as follows:

test.onkeydown = function(){
return false;
}

test.onkeyup = function(){
return false;
}

test.onkeypress = function(){
return false;
}

We comment out two of them respectively events, each test only binds one event.
Obviously each of our functions returns false. If the return value can prevent the default action of the event, then the text box will not be able to enter anything.
Look at the results of my test below, pay attention to the red part.
Finally, I bound the event twice and returned false each time to see if the default action could be prevented.
I still use an a tag to test whether onclick returns false and jumps.
侦听事件返回 false 是否阻止事件默认动作
  chrome IE-8 firfox oper Safari
onkeydown yes yes yes no yes
onkeyup no no no no no
onkeypress yes yes yes yes yes
onclick yes yes yes yes yes
keydown * 2 no 取最后的FN结果 no no no
keypress * 2 no 取最后的FN结果 no no no
click * 2 no 取最后的FN结果 no no no
e.preventDefault(); yes no yes yes(keydown:no) yes
e.returnValue = false no yes no no no

It can be seen that the performance of browsers is indeed different. Of course, IE is the most troublesome thing.
The most surprising thing is that binding down in oper returns false, but it cannot prevent the default action.
So in the future, when writing to prevent the browser’s default action, it is better to use the standard method. (I will provide it later)
Otherwise, it will be quite troublesome in multi-person collaboration work.
If you need the demo demo, you can send it to me via email. I won't post it.
Copy code The code is as follows:

/* * Tragedy will not happen if you use the following code
* Final conclusion
* E(e).stop(); Prevent time bubbling
* E(e).prevent(); Prevent time default behavior
*/
var E = function(e){
e = window.event || e;
return {
stop: function() {
if (e && e.stopPropagation) e.stopPropagation();
else e.cancelBubble = true
},
prevent: function() {
if (e && e.preventDefault) e.preventDefault();
else e.returnValue = false
}
}
}
Statement:
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