代码描述:for语句 批量注册事件 处理函数。for语句批量注册事件处理函数代码实例
本章节介绍一下如何利用for语句实现批量注册事件处理函数。 如果遇到需要注册事件处理函数的元素较多的时候,批量注册将会省却很多代码。 代码实例如下:
<!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <meta name="author" content="https://www.51qianduan.com/" /> <title>51前端</title> <script type="text/javascript"> window.onload=initAll; function initAll(){ var clearText = document.getElementsByTagName("input"); for(var i=0;i<clearText.length;i++){ clearText[i].onfocus=function(){ if(this.value==this.defaultValue){ this.value=""; } } clearText[i].onblur = function () { if(this.value==""){ this.value=this.defaultValue; } } } } </script> </head> <body> <input type="text" name="text" value="51前端一"/> <input type="text" name="text" value="51前端二"/> <input type="text" name="text" value="51前端三"/> </body> </html>
请绑定手机号,在继续操作
注意:只有绑定手机以后,才可使用网站全部功能