Home > Article > Web Front-end > JS implements dynamically adding events to label controls
This article mainly introduces the JS method of dynamically adding events to label controls, and analyzes the relevant operating techniques of JavaScript to simply implement dynamically added events in the form of examples. Friends in need can refer to the examples of this article
Describes the JS method of dynamically adding events to label controls. 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="http://www.w3.org/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:
Steps to implement dynamic progress bar with JS Analysis
D3.js implements dynamic dial effect
js implements dynamic process progress display bar
The above is the detailed content of JS implements dynamically adding events to label controls. For more information, please follow other related articles on the PHP Chinese website!