Why do you have to pass the event object as a parameter?

后端 未结 5 1457
没有蜡笔的小新
没有蜡笔的小新 2020-12-28 10:37

I\'m learning how to manipulate events in JavaScript and I\'m wondering \"why do you have to pass the event object as a parameter (argument) into a function when using event

5条回答
  •  旧巷少年郎
    2020-12-28 10:59

    You are not passing the event parameter anywhere. You are just making a function that takes one parameter, called event.

    When the browser calls the event handlers, it calls the function(s) assigned to it, and passes the event object to it as the 1st parameter.

    P.S. You don't need the () around your function.

    document.getElementById('button_1').onclick = function (event) {
        alert("The event is: " + "on" + event.type);
    };
    

提交回复
热议问题