jQuery focusout event doesn't trigger on table element

北慕城南 提交于 2019-12-12 18:09:18

问题


I'm trying to make a table support multiple row selection (for the moment just the CTRL + mouseclick combination). Everything works fine, but when I click outside the table area, the rows don't deselect. Unfortunately, I have found out that the focusout event doesn't trigger at all. Here's my code:

    $(".library tbody tr").live('click', function (event) {
        event.preventDefault();

        if (event.ctrlKey) {
            $(this).toggleClass('selected-row');
        } else {
            $(".library tbody tr").removeClass("selected-row");
            $(this).addClass("selected-row");
        }
    });

    $("table.library").live('click', function () {
        $(".library").addClass("focused");
    });

    $("table.library").live('focusout', function () {
        $(this).removeClass("focused");
    });

Has anyone else dealt with this issue? Thank you in advance!


回答1:


The 'focusout' event only fires when focus leaves elements which can be focused in the first place, such as input elements. A table element can't get that focus, as far as I know. You'd need to handle the deselection of selected rows in some other way, Lollero mentoned one possible solution in his comment on your question.




回答2:


Table element can also get focus if you use tabindex for table body or table element as

$('#tableId tbody').attr("tabindex", 1);

After this when you click on table it will get focus.



来源:https://stackoverflow.com/questions/10171313/jquery-focusout-event-doesnt-trigger-on-table-element

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