Home > Web Front-end > JS Tutorial > body text

JS implements dynamically adding events to label controls

不言
Release: 2018-05-05 16:05:07
Original
2550 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 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(&#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:

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!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!