Method: 1. Use the event attribute onclick of the HTML tag to bind the handler, with the syntax "onclick="event handler""; 2. Use the event attribute of the event source to bind the handler, with the syntax "obj. on event name = handler function"; 3. Use addEventListener() to bind the handler.
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
In order for the browser to automatically call the corresponding event handler to handle the event when the event occurs, it is necessary to bind the event handler to the event source (the bound event handler is also called a registered event handler).
There are 3 ways to bind event handlers:
1. Use the event attribute of the HTML tag to bind the handler.
It should be noted that when using the event attribute of the HTML tag to bind the event handler, the event The script code in the attribute cannot contain a function declaration, but can be a function call or a series of script codes separated by semicolons.[Example 1] Bind an event handler using the event attribute of the HTML tag .
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>使用HTML标签的事件属性绑定事件处理程序</title> </head> <body> <input type="button" onclick="var name='PHP中文网';alert(name);" value="事件绑定测试"/> </body> </html>
[Example 2] The event attribute of the HTML tag is a function call.
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>HTML标签的事件属性为函数调用</title> <script> function printName(){ var name = "PHP中文网"; alert(name); } </script> </head> <body> <input type="button" onClick="printName()" value="事件绑定测试"/> </body> </html>
2. Bind handlers using event attributes of event sources
One of the ways to separate HTML and JS is by using event sources. The event attribute binds the event processing function. The binding format is as follows:obj.on事件名 = 事件处理函数
oBtn.onclick = function(){//oBtn为事件源对象,它的单击事件绑定了一个匿名函数定义 alert('hi') };
[Example 3]Use the event attribute of event source to bind event handler function.
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>使用事件源的事件属性绑定事件处理函数</title> <script> window.onload = function(){//窗口加载事件绑定了一个匿名函数 //定义一个名为fn的函数 function fn(){ alert('hello'); } //获取事件源对象 var oBtn1 = document.getElementById("btn1"); var oBtn2 = document.getElementById("btn2"); //绑定一个匿名函数 oBtn1.onclick = function(){ alert("hi"); } //绑定一个函数名 oBtn2.onclick = fn; }; </script> </head> <body> <input type="button" id="btn1" value="绑定一个匿名函数"> <input type="button" id="btn2" value="绑定一个函数名"> </body> </html>
##3. Use addEventListener() to bind the handlerUsing the event attribute of the event source object to bind event handlers is simple, but it has a shortcoming: an event can only be bound to one handler, and the event handler bound later will overwrite the previously bound event. processing function. In actual applications, an event from an event source may be processed by multiple functions.
当一个事件源需要使用多个函数来处理时,可以通过事件源调用 addEventListener()(针对标准浏览器)来绑定事件处理函数以实现此需求。一个事件源通过方法绑定多个事件函数的实现方式是:对事件源对象调用多次 addEventListener(),其中每次的调用只绑定一个事件处理函数。
addEventListener() 是标准事件模型中的一个方法,对所有标准浏览器都有效。使用 addEvent Liste ner() 绑定事件处理程序的格式如下:
事件源.addEventListener(事件名称,事件处理函数名,是否捕获);
参数“事件名称”是一个不带“on”的事件名;参数“是否捕获”是一个布尔值,默认值为 false,取 false 时实现事件冒泡,取 true 时实现事件捕获。
通过多次调用 addEventListener() 可以为一个事件源对象的同一个事件类型绑定多个事件处理函数。当对象发生事件时,所有该事件绑定的事件处理函数就会按照绑定的顺序依次调用执行。另外,需要注意的是,addEventListener() 绑定的事件处理函数中的 this 指向事件源。
addEventListener() 绑定处理程序示例:
document.addEventListener('click',fn1,false);//click事件绑定fn1函数实现事件冒泡 document.addEventListener('click',fn2,true);//click事件绑定fn2函数实现事件捕获
【例 4】使用 addEventListener() 绑定事件函数。
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>使用addEventListener()/attachEvent()绑定事件函数</title> <script> function fn1(){ alert("fn1()"); } function fn2(){ alert("fn2()"); } function bindTest(){ document.addEventListener('click',fn1,false);//首先绑定fn1函数 document.addEventListener('click',fn2,false); } bindTest();//调用函数 </script> </head> <body> </body> </html>
上述代码在浏览器中运行后,当单击文档窗口时,会依次弹出显示“fn1()”和“fn2()”的警告对话框。
【推荐学习:javascript高级教程】
The above is the detailed content of How to bind events in javascript. For more information, please follow other related articles on the PHP Chinese website!