手抄报 安全手抄报 手抄报内容 手抄报图片 英语手抄报 清明节手抄报 节约用水手抄报

js给onclick赋值,传参数

时间:2024-10-13 18:15:10

1、第一种:button.onclick = Function("alert('hello');");第二种:button.onclick =function(){alert("hello"); };第三种:button.onclick = myAlert;functionmyAlert(){alert("hello");}

2、第四种:这种情况更加动态,更为实用,而且还能添加多个函数(添加的事件的顺序即执行顺序),呵呵if(window.addEventListener){ // Mozilla, Netscape, Firefox//element.addEventListener(type,listener,useCapture);button.addEventListener('click', alert('11'), false);button.addEventListener('click', alert('12'), false);//执行顺序11 -> 12} else { // IEbutton.attachEvent('onclick', function(){alert('21');});button.attachEvent('onclick', function(){alert('22');});执行顺序22 -> 21}

3、实例讲解:button.onclick =Function(&孥恶膈茯quot;alert('31');");button.onclick =Function("alert('32');");button.onclick =Function("alert('33');"); //如果这样写,那么将会只有最后一个方法被执行button.attachEvent("onclick", function(){alert('41');});button.attachEvent("onclick", function(){alert('42');});button.attachEvent("onclick", function(){alert('43');}); //如果这样写,三个方法都会被执行//当然,你也可以这样写button.onclick = Function("alert('51');");button.attachEvent("onclick", function(){alert('52');});//对应移除事件detachEvent('onclick',func);//ie下使用删除事件funcremoveEventListener('click',func);//Mozilla下,删除事件func

© 手抄报圈