jQuery - close dropdown with .mouseout or a more suitable method, hint?

浪子不回头ぞ 提交于 2019-12-04 05:41:25

问题


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:

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:


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?



来源:https://stackoverflow.com/questions/15343793/jquery-close-dropdown-with-mouseout-or-a-more-suitable-method-hint

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!