The easiest thing is this:
<input type="button" onclick="alert()" value="I am a button" />
Dynamically add onclick event:
<input type="button" value="I am a button" >
<script type="text/javascript">
var bObj=("bu");
= objclick;
function objclick(){alert()};
</script>
If you use the anonymous function function(){}, it is as follows:
<input type="button" value="I am a button" >
<script type="text/javascript">
var bObj=("bu");
=function(){alert()};
</script>
The above methods actually have the same principles, and they all define the value of the onclick attribute. It is worth noting that if you define multiple times, such as: =method1; =method2; =method3, then only the last definition =method3 will take effect, and the first two definitions are overwritten by the last one.
Let’s look at the attachEvent in IE again:
<input type="button" value="I am Laden" >
<script type="text/javascript">
var bObj = ("bu");
("onclick",method1);
("onclick",method2);
("onclick",method3);
function method1(){alert("first alert")}
function method2(){alert("second alert")}
function method3(){alert("third alert")}
</script>
The execution order is method3 > method2 > method1, first in and then out, similar to the variables in the stack. It should be noted that the first parameter in attachEvent starts with on, which can be onclick/onmouseover/onfocus, etc.
It is said that (unconfirmed verified) it is best to use detachEvent in IE to free memory after using attachEvent.
Let’s take a look at the addEventListener in Firefox:
<input type="button" value="I am Bush" >
<script type="text/javascript">
var bObj = ("bu");
("click",method1,false);
("click",method2,false);
("click",method3,false);
function method1(){alert("first alert")}
function method2(){alert("second alert")}
function method3(){alert("third alert")}
</script>
As you can see, the execution order in ff is method1> method2> method3, which is exactly the opposite of IE, first in, first out. It should be noted that addEventListener has three parameters. The first is the event name without "on", such as click/mouseover/focus, etc.
<input type="button" onclick="alert()" value="I am a button" />
Dynamically add onclick event:
<input type="button" value="I am a button" >
<script type="text/javascript">
var bObj=("bu");
= objclick;
function objclick(){alert()};
</script>
If you use the anonymous function function(){}, it is as follows:
<input type="button" value="I am a button" >
<script type="text/javascript">
var bObj=("bu");
=function(){alert()};
</script>
The above methods actually have the same principles, and they all define the value of the onclick attribute. It is worth noting that if you define multiple times, such as: =method1; =method2; =method3, then only the last definition =method3 will take effect, and the first two definitions are overwritten by the last one.
Let’s look at the attachEvent in IE again:
<input type="button" value="I am Laden" >
<script type="text/javascript">
var bObj = ("bu");
("onclick",method1);
("onclick",method2);
("onclick",method3);
function method1(){alert("first alert")}
function method2(){alert("second alert")}
function method3(){alert("third alert")}
</script>
The execution order is method3 > method2 > method1, first in and then out, similar to the variables in the stack. It should be noted that the first parameter in attachEvent starts with on, which can be onclick/onmouseover/onfocus, etc.
It is said that (unconfirmed verified) it is best to use detachEvent in IE to free memory after using attachEvent.
Let’s take a look at the addEventListener in Firefox:
<input type="button" value="I am Bush" >
<script type="text/javascript">
var bObj = ("bu");
("click",method1,false);
("click",method2,false);
("click",method3,false);
function method1(){alert("first alert")}
function method2(){alert("second alert")}
function method3(){alert("third alert")}
</script>
As you can see, the execution order in ff is method1> method2> method3, which is exactly the opposite of IE, first in, first out. It should be noted that addEventListener has three parameters. The first is the event name without "on", such as click/mouseover/focus, etc.