简体   繁体   English

jQuery - 焦点/模糊显示/隐藏输入组

[英]jQuery - Focus/Blur to Show/Hide input groups

I have a little form with 2 groups of 3 inputs. 我有一个小组,有2组3输入。

Only one input is visible for each group. 每组只能看到一个输入。

没有投入集中

If I focus an input, all in the same group are shown. 如果我关注输入,则会显示同一组中的所有内容。

Group1输入集中

If no input of the group is focused, only the first one should appear. 如果没有聚焦该组的输入,则只应出现第一个。

Group2输入集中

$('input').focus(function () {
    $(this).closest('div').addClass('focused');
}).blur(function () {
    $(this).closest('div').removeClass('focused');
})

This works in Chrome but not in Firefox (please check my fiddle ). 这适用于Chrome,但不适用于Firefox(请查看我的小提琴 )。
Both right hand side inputs are hidden before getting the focus. 在获得焦点之前,两个右侧输入都被隐藏。
Seems like events bubbling order is different on both browsers. 似乎事件冒泡顺序在两个浏览器上都不同。

Can someone help me to make this work for all browsers ? 有人可以帮助我为所有浏览器做这项工作吗?

You could add a slight delay to prevent this. 你可以添加一点延迟来防止这种情况。

$('input').focus(function () {
    var $parent = $(this).closest('div');
    var timeoutId = $parent.data('tid');

    if (timeoutId) {
        // Aborting the blur
        clearTimeout(timeoutId);
    }

    $parent.addClass('focused');
}).blur(function () {
    var $parent = $(this).closest('div');

    var tid = setTimeout(function(){
        $parent.removeClass('focused');
    }, 1);

    $parent.data('tid', tid);
})

http://jsfiddle.net/b439u/5/ http://jsfiddle.net/b439u/5/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM