简体   繁体   English

jQuery的模糊,但专注于另一个

[英]jquery on blur but focus on the other one

I have a situation that i have a hidden div with two input elements where it hides by default and only shows up if I hover something and disappears back if I hover out. 我有一个情况,我有一个带有两个输入元素的隐藏div,它在默认情况下是隐藏的,并且仅当我将内容悬停时才显示,而当我悬停时才消失。 Now I want that div not to disappear if one of the input element is focused and disappears if blurred. 现在,我希望该div如果输入元素之一被聚焦而不会消失,而如果模糊则消失。 I have solved that part until, if the focus is transferred from one input to another, the div will disappear which I don't want it to disappear because one of the input is still focused. 我已经解决了这一部分,直到,如果焦点从一个输入转移到另一个输入,div将消失,而我不希望它消失,因为其中一个输入仍在聚焦。

Here's my code: 这是我的代码:

// code with the problem I think.
$('#inputText1, #inputText2').live("blur", function() {
    if ($('#myDiv').is(":visible")) {
        if (!$(this).hasClass("jqTransformInputWrapper_focus")) {
            $('#myDiv').fadeOut("slow");
        }
    }
});

// hover to show and hide the div
$(".visibleDiv").hover(
    function() {
        $('#myDiv').fadeIn("slow");
    },
    function() {
        if(!$(this).find(".jqTransformInputWrapper").hasClass("jqTransformInputWrapper_focus")) {
            $('#myDiv').fadeOut("slow");
        }
    }
);

My Html: 我的HTML:

<div class="visibleDiv">
    hover me
    <div id="myDiv">
        <input name="inputText1" id="inputText1" type="text" />
        <input name="inputText2" id="inputText2" type="text" />
    </div>
</div>

BTW, I'm using jqTransform for the input elements. 顺便说一句,我正在使用jqTransform作为输入元素。

try this, 尝试这个,

// code with the problem I think.
$('#inputText1, #inputText2').live("blur", function() {

    if ($('#myDiv').is(":visible")) {
        if (!$(this).hasClass("jqTransformInputWrapper_focus")) {
            $('#myDiv').fadeOut("slow");
        }
    }

}).live("focus",function(){
    $('#myDiv').stop(true,true).show();
});

give some class to input box and apply jQuery on that 给一些类输入框并在其上应用jQuery

<div class="visibleDiv">
    hover me
    <div id="myDiv">
        <input name="inputText1" id="inputText1" type="text" class="inputbox" />
        <input name="inputText2" id="inputText2" type="text" class="inputbox"/>
    </div>
</div>


// now this will work
$('.inputbox').live("blur", function() {
 // u can also use this
 // if($(this).parent().is(":visible") )
    if ($('#myDiv').is(":visible")) {
        if (!$(this).hasClass("jqTransformInputWrapper_focus")) {
            $('#myDiv').fadeOut("slow");
        }
    }
});

Note: use delegate instead of live 注意:使用委托代替实时

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

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