zoukankan      html  css  js  c++  java
  • 跨浏览器处理事件

    <!DOCTYPE html>
    <!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">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题文档</title>
    
    </head>
    </body>
    <div id="box">
    	<input type="button" value="按钮1" id="bt" onclick="song()" />
        <input type="button" value="按钮2" id="bt2"  />
        <input type="button" value="按钮3" id="bt3"  />
    
    解决各个浏览器不兼容的问题
    <script>
    	function song(){
    		alert("Hello");
    	}
    	
    	var bt2 = document.getElementById('bt2');
    	var bt3 = document.getElementById('bt3');
    	//bt2.onclick = song;
    	//bt2.onclick = null;
    	
    	//dom2级事件
    	//bt3.addEventListener('click',function(){alert(this.value);},false);
    	//bt3.removeEventListener('click',song,false);
    	//bt3.attachEvent('onclick',song);
    	//bt3.detachEvent('onclick',song);
    	
    	
    	//跨浏览器
    	var kua={
    		//加入句柄
    		 addHandler:function(element,type,handler){
    		 if(element.addEventListener){
    			element.addEventListener(type,handler,false);
    		}else if(element.attachEvent){
    			element.attachEvent('on'+type,handler);
    		}else{
    			element['on'+type] = handler;
    		  }
    		},
    		//删除句柄
    		removeHandler:function(element,type,handler){
    			if(element.removeEventListener){
    				element.removeEventListener(type,handler,false);
    			}else if(element.detachEvent){
    				element.detachEvent('on'+type,handler);
    			}else{
    				element['on'+type] = null;
    			}
    		}
    	}
    	kua.addHandler(bt2,'click',song);
    	kua.removeHandler(bt2,'click',song);
    </script>
    </div>
    
    </body>
    </html>
    

  • 相关阅读:
    机器学习中的特征缩放(feature scaling)
    vs未能正确加载XXX包,编译时停止工作问题
    Python基础-画图:matplotlib
    深度学习基本知识
    TensorFlow入门:线性回归
    SQLserver中取众位数的写法
    python pip安装报错: ConnectTimeoutError
    TensorFlow安装-Windows
    Python读文件报错:SyntaxError: Non-ASCII character in file
    java 项目连接MySQL数据库
  • 原文地址:https://www.cnblogs.com/zfyouxi/p/5360289.html
Copyright © 2011-2022 走看看