2015-07-11 85 views
0

此代码在Firefox中不工作,但在IE和Chrome上工作。当我在文本框内单击时,它会运行focusOut()focusIn()函数。这种行为是否是预期的,并且是推荐的语法?将JavaScript事件添加到HTML输入

<!doctype html> 
 

 
<html> 
 
    <head> 
 
    <title>Page of Cage</title> 
 
    <meta charset="UTF-8"> 
 
    <link rel="icon" href="mkx-logo.png" type="image/x-icon"> 
 
    <script type="text/javascript"> 
 
     function focusIn() { 
 
     alert("focus In!"); 
 
     }; 
 

 
     function focusOut() { 
 
     alert("focus OUT!"); 
 
     }; 
 
    </script> \t 
 
    </head> 
 
    <body> 
 
    <form> \t 
 
     Name:<input onfocus="focusIn()" onblur ="focusOut()" type="text" name="name"/> <br/> 
 
     <input type="submit" value ="submit"/> \t \t \t 
 
    </form> 
 
    </body> 
 
</html> \t

+0

这应该在所有的浏览器,因为一切看起来正确的给我。 –

+0

FF会在控制台中给你一个错误吗? – nnnnnn

+0

控制台没有错误。也许这是逻辑错误,它同时显示focusIn和focusOut函数。 – thinker

回答

2

更换alertconsole.log和使用的开发工具看你的网页浏览器的控制台。就像nnnnnn在评论中说的那样。

0

问:代码为什么不起作用?
答:onfocus会在焦点丢失时触发,onblur会被触发。在您的代码中,当您专注于文本框时,它会生成onfocus,并且同时生成alert box并失去焦点。所以,这两个功能都启动了。

Check out this Fiddle