First: Button.onclick = Function ("alert (' Hello ');");
The second type: Button.onclick = function () {alert ("Hello");
The third type: Button.onclick = Myalert;
function Myalert () {
Alert ("Hello");
}
The fourth kind:
This situation is more dynamic, more practical, but also can add more than one function (add the sequence of events is the order of execution), hehe
if (Window.addeventlistener) {//Mozilla, Netscape, Firefox
Element.addeventlistener (type,listener,usecapture);
Button.addeventlistener (' Click ', alert (' One '), false);
Button.addeventlistener (' Click ', Alert ('), false);//execution Order-> 12
else {//IE
Button.attachevent (' onclick ', function () {alert (' 21 ');});
Button.attachevent (' onclick ', function () {alert (' 22 ');}); Order of Execution-> 21
}
Example Explanation:
Button.onclick = Function ("alert (' 31 ');");
Button.onclick = Function ("alert (' 32 ');");
Button.onclick = Function ("alert (' 33 ');"); If you write this, then only the last method will be executed
Button.attachevent ("onclick", function () {alert (' 41 ');});
Button.attachevent ("onclick", function () {alert (' 42 ');});
Button.attachevent ("onclick", function () {alert (' 43 ');}); If you write this, three methods will be executed
Of course, you can also write this
Button.onclick = Function ("alert (' 51 ');");
Button.attachevent ("onclick", function () {alert (' 52 ');});
Corresponds to the removal event
DetachEvent (' onclick ', func);//ie using Delete event func
RemoveEventListener (' click ', func);//mozilla, delete event func