Home  >  Article  >  Web Front-end  >  JavaScript method to determine whether a button is clicked_javascript skills

JavaScript method to determine whether a button is clicked_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:25:542156browse

No more nonsense, let me just post the code for you.

firfox version:

<HTML> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<HEAD> 
<TITLE></TITLE> 
<script> 
document.onclick=function(e){ 
var evt=e||window.event; 
var tar=evt.target||evt.srcElement; 
if( (tar.tagName.toLowerCase()=="input"&&tar.type=="button") || tar.tagName.toLowerCase()=="button"){ 
alert("你点击的是一个按钮") 
} 
} 
</script> 
</HEAD> 
<BODY> 
<input id="button1" type="button" value="1111"/> 
<input id="button2" type="button" value="2222"/> 
<input id="button3" type="button" value="3333"/> 
<input id="button4" type="button" value="4444"/> 
</BODY> 
</HTML> 
google 浏览器版:
<HTML> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<HEAD> 
<TITLE></TITLE> 
<script> 
document.onclick=function()
{ var obj = event.srcElement; 
if(obj.type == "button"){ 
alert(obj.id); } 
} 
</script> 
</HEAD> 
<BODY> 
<input id="button1" type="button" value="1111"/> 
<input id="button2" type="button" value="2222"/> 
<input id="button3" type="button" value="3333"/> 
<input id="button4" type="button" value="4444"/> 
</BODY> 
</HTML> 

The above is the JavaScript method that the editor shared with you to determine when the button is clicked. I hope you like it.

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