阅读背景:

JS动态添加事件

来源:互联网 

<textarea cols="50" rows="15" name="code" class="javascript">&lt;mce:script language=&quot;javascript&quot;&gt;&lt;!-- function set() { var obj = document.getElementById(&quot;fy&quot;); //obj.attachEvent('onfocus', add); // 在原先事件上添加 //obj.setAttribute('onfocus',add); //会替代原有事件方法 //obj.onfocus=add; //等效 obj.setAttribute('onfocus',add); if(window.addEventListener) { // 其它浏览器的事件代码: Mozilla, Netscape, Firefox //添加的事件的顺序即执行顺序 //注意用 addEventListener 添加带on的事件,不用加on obj.addEventListener('focus', add, false); } else { //IE 的事件代码 在原先事件上添加 add 方法 obj.attachEvent('onfocus',add); } } /////////////////////////////注意 /////////////////////////////////////////////////// //注意add这里不能带参数,add()这样写也不行 //要带参数的话就用attachEvent(&quot;onclick&quot;,new Function('add(&quot;hello&quot;)')) /////////////////////////////// 注意//////////////////////////////////////////////////// function add(){alert(&quot;已经成功添加事件&quot;);} // --&gt;&lt;/mce:script&gt; </textarea><textarea cols="50" rows="15" name="code" class




你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: