This article mainly introduces the JS method of dynamically adding events to label controls, and analyzes the relevant operating techniques of javascript in simple implementation of dynamically adding events in the form of examples. Friends in need can refer to the following
The example of this article describes the method of dynamically adding events to label controls using JS. Share it with everyone for your reference, the details are as follows:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="m.sbmmt.com/1999/xhtml"> <script language="javascript"> function set(){ var obj = document.getElementById("fy"); //obj.attachEvent('onfocus', add); //在原先事件上添加 //obj.setAttribute('onfocus',add); //会替代原有事件方法 //obj.onfocus=add; //等效obj.setAttribute('onfocus',add); if (window.addEventListener) { //其它浏览器的事件代码: Mozilla, Netscape, Firefox //添加的事件的顺序即执行顺序 //注意用 addEventListener 添加带on的事件,不用加on obj.addEventListener('focus', add, false); } else { //IE 的事件代码 在原先事件上添加 add 方法obj.attachEvent('onfocus', add); } } function add() { alert("已经成功添加事件"); } </script> <body> <input type="text" onfocus="alert('预设事件');" id="fy" /> <input type="button" onclick="set();" value="sssss"/> </body> </html>
[Related recommendations]
1. Special recommendation: "php Programmer Toolbox" V0.1 version download
2. Free js online video tutorial
3. php.cn Dugu Jiujian (3) - JavaScript video tutorial
The above is the detailed content of Relevant techniques for dynamically adding events using js. For more information, please follow other related articles on the PHP Chinese website!