jQuery:如何在onblur之前先点击onclick? [英] jQuery: How to fire onclick first before onblur?

查看:193
本文介绍了jQuery:如何在onblur之前先点击onclick?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我目前专注于jQuery中的文本框,如何在onblur事件之前首先触发onclick事件?

How can I fire the onclick event first before the onblur event if I'm currently focused at the textbox in jQuery?

预期输出:在texbox中的模糊事件之前首先触发按钮单击事件。提醒mybtn onclick alert

Expected Output: Fire the button click event first before the blur event in the texbox. Alerts "mybtn onclick alert"

演示: http://jsfiddle.net/Zk8Fg/

代码:

<input type="text" name="fname" id="fname" value="" /><br />
<input type="button" name="mybtn" id="mybtn" value="Click Me" />

<script language="javascript">
$(document).ready(function() {
    myFunc();
});

function myFunc() {
    $('#fname').focus().blur(function() {
        alert('fname onblur alert'); 
    });

    $('#mybtn').click(function() {
        alert('mybtn onclick alert'); 
    });
}
</script>

谢谢:)

推荐答案

嗯,它可能是标准事件的一部分,因为你可能不应该更改订单。你可以做的是使用 window.setTimeout 推迟产生的动作,但有一个短暂的延迟。

Well it's probably part of the standard which event fires first so you probably shouldn't be changing the order. What you could do though is postpone the resulting action using window.setTimeout with a short delay.

$('#fname').focus().blur(function() {
    window.setTimeout(function(){alert('fname onblur alert')},0.1); 
});

我同意其他评论,但看起来你应该重新考虑一下你的情况这是因为重新排序默认事件让我觉得这是一个可能有更优雅解决方案的蹩脚黑客。

I agree with the other comments though, it really looks like you should rethink how you're going about this because reordering default events strikes me as a lame hack that probably has more elegant solutions.

这篇关于jQuery:如何在onblur之前先点击onclick?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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