jQuery - close dropdown with .mouseout or a more s

2019-03-04 09:42发布

I have the following piece of code:

$(".option_box .option_name").click(function () {
    $(this).siblings(".collapsible").toggle();
    $(this).toggleClass("hided");
});

$(".option_box .attribute_group_name").click(function () {
    $(this).siblings(".attribute_box").toggle();
    $(this).toggleClass("hided");
});

The HTML code associated with it is:

<div class="option_box" style="left:140px;">
    <div class="option_name">Gama</div>
    <table class="collapsible">
        <tr>
            <td><input id="gama_1" class="gama_value filtered" type="checkbox" name="gama[]" value="1" /></td>
            <td><label for="gama_1">Correcta</label></td>
        </tr>
        <tr>
            <td><input id="gama_2" class="gama_value filtered" type="checkbox" name="gama[]" value="2" /></td>
            <td><label for="gama_2">Aficionado</label></td>
        </tr>
        <tr>
            <td><input id="gama_3" class="gama_value filtered" type="checkbox" name="gama[]" value="3" /></td>
            <td><label for="gama_3">Entusiasta</label></td>
        </tr>
        <tr>
            <td><input id="gama_4" class="gama_value filtered" type="checkbox" name="gama[]" value="4" /></td>
            <td><label for="gama_4">Purista</label></td>
        </tr>
        <tr>
            <td><input id="gama_5" class="gama_value filtered" type="checkbox" name="gama[]" value="5" /></td>
            <td><label for="gama_5">Exclusive</label></td>
        </tr>
    </table>
</div>

Which, visually produces this:

a view of the dropdown-style filter

My problem is I want for the dropdown (which, in practical reality, is a div element with a table inside) to close when I move the mouse out of it but, as you can see, it's not that easy, as it's not a matter of declaring a .mouseout event like I did in the beginning.

The dropdown is composed of two parts, the 'header' and the 'body' and I want for the whole dropdown to close itself when I abandon either one or the other but, so far, no luck. I lack some real knowledge in jQuery (just a bare touch here and there, but nothing stellar for the moment), so I'm confused. I tried .mouseout, .mouseleave, .focusout and other methods.

I've even resorted to jQuery++ to use the .within method but I'm equally lost. Could any of you point me to the right direction?

1条回答
Bombasti
2楼-- · 2019-03-04 10:28

As i understood: you would like to collapse table with mouseout. Why don't you add something like this:

$(".collapsible").mouseleave(function() {
    $(".collapsible").hide();
})

doesn't it work for you?

查看更多
登录 后发表回答