为什么这个jQuery事件运行两次? [英] Why does this jQuery event run twice?

查看:110
本文介绍了为什么这个jQuery事件运行两次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您运行此代码并单击 input2 时,它将获得焦点, .result div将输出f2一次。但是当您单击 input1 时,该脚本将让 input2 获取焦点, .result div将输出f2两次,为什么?

When you run this code and click input2, it will get the focus and .result div will output "f2" once. But when you click input1, the script will let input2 get the focus and .result div will output "f2" twice, why?

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>blank</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){   
    $(".input1").click(function(){$(".input2").focus();});
    $(".input2").focus(function(){$(".result").html($(".result").html()+"f2, ");});
});
</script>
</head>

<body>
    <p>
        <input class="input1" value="click me to set the input2's focus" />
        <input class="input2" value="input2" />
        <div class="result"></div>
    </p>
</body>
</html>


推荐答案

这是一个已知的jquery错误:

This is a known jquery bug :

http://bugs.jquery.com/ticket/6705

IE错误地调用焦点两次。

IE incorrectly calls the focus twice.

这篇关于为什么这个jQuery事件运行两次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆