简体   繁体   中英

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. Now I want that div not to disappear if one of the input element is focused and disappears if blurred. 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.

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:

<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.

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

<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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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