文本框闪烁

<!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 style="text-align:center" onload="init();">

      <input type="text"  /><br /><br />

      <input type="text"  />

      <script type="text/javascript">

         function init(){

                 var d=document.getElementsByTagName("input");

                           for(var i=0;i<d.length;i++){


                                  d[i].onfocus=function(){

                                    timer=setInterval(function(){ 

                                          var c=this.style.borderColor;

                                          if(c=="yellow")

                                         

                                              this.style.borderColor="";

                                          }

                                          else

                                          {  

                                                  this.style.borderColor="yellow";

                                          }

                                    },1000)

                                                        }

                                  d[i].onblur=function(){

                                            clearInterval(timer);

                                            this.style.borderColor="";

                                                       }

                                                   }                                                 

                         } 

 </script>

</body>

</html>


qq_小盛开_0
浏览 1339回答 2
2回答

stone310

中间focus事件函数修改如下注释:d[i].onfocus=function(){     var self=this;             //保存this,这里this指的是d[i]     timer=setInterval(function(){         var c=self.style.borderColor;  //用刚才保存的this值,因为这里的this值就是window,而不是d[i]了         if(c=="yellow")         {             self.style.borderColor="";         }         else         {             self.style.borderColor="yellow";         }     },1000) }

皓韵儿

??你确定吗?为什么我复制粘贴的没有闪烁? 是不是你没有保存呀?保存试试,实在不行就重新打开你的编译器试试。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript