为了账号安全,请及时绑定邮箱和手机立即绑定

文本框闪烁

文本框闪烁

qq_小盛开_0 2016-10-27 20:30:10
<!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>
查看完整描述

2 回答

已采纳
?
stone310

TA贡献361条经验 获得超191个赞

中间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)
}


查看完整回答
反对 回复 2016-10-28
  • qq_小盛开_0
    qq_小盛开_0
    在if中self.style.borderColor改成c为什么不可以
  • stone310
    stone310
    什么意思,这里不已经是 if(c=="yellow")吗,能运行啊
  • qq_小盛开_0
    qq_小盛开_0
    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") { c=""; } else { c="yellow"; } },1000) 我把它改成这样不能运行的原因是什么?
点击展开后面2
?
皓韵儿

TA贡献86条经验 获得超114个赞

??你确定吗?为什么我复制粘贴的没有闪烁? 是不是你没有保存呀?保存试试,实在不行就重新打开你的编译器试试。

查看完整回答
1 反对 回复 2016-10-27
  • 2 回答
  • 0 关注
  • 1291 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信