原生js为元素添加事件

1.行间事件

   onclick="函数名()";

<div id="div1" onmouseover="over('400px','400px','green')"
	       onmouseout="over('200px','200px','red')"
onclick="show()"></div>

2.js中加事件  设置函数,发生事件时才触发

匿名函数

                       obj.onmouseout=function(){
	   			//此处可用this 应为obj调用者已确定
				this.style.width="400px";
				this.style['background-color']="green";
	   		}
			
			obj.onmouseover=function(){
				this.style.width="200px";
				this.style['background-color']="red";
			};

  js中加事件 非匿名函数   onclick=函数名;

function s(){
        ....          
}

obj.onclick=s;

  

原文地址:https://www.cnblogs.com/tianxiaoxiao/p/8719936.html