Home > Web Front-end > JS Tutorial > Relevant techniques for dynamically adding events using js

Relevant techniques for dynamically adding events using js

零下一度
Release: 2017-05-16 09:46:52
Original
1133 people have browsed it

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="www.php.cn/1999/xhtml">
<script language="javascript">
function set(){
  var obj = document.getElementById("fy");
  //obj.attachEvent(&#39;onfocus&#39;, add); //在原先事件上添加
  //obj.setAttribute(&#39;onfocus&#39;,add); //会替代原有事件方法
  //obj.onfocus=add;
  //等效obj.setAttribute(&#39;onfocus&#39;,add);
  if (window.addEventListener) {
    //其它浏览器的事件代码: Mozilla, Netscape, Firefox
    //添加的事件的顺序即执行顺序
    //注意用 addEventListener 添加带on的事件,不用加on
    obj.addEventListener(&#39;focus&#39;, add, false);
  } else {
    //IE 的事件代码 在原先事件上添加 add
    方法obj.attachEvent(&#39;onfocus&#39;, add);
  }
}
function add() {
  alert("已经成功添加事件");
}
</script>
<body>
  <input type="text" onfocus="alert(&#39;预设事件&#39;);" id="fy" />
  <input type="button" onclick="set();" value="sssss"/>
</body>
</html>
Copy after login

[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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template