Hide a DIV when it loses focus/blur

后端 未结 11 778
长情又很酷
长情又很酷 2021-02-05 08:34

I have a JavaScript that displays a DIV (sets its display css property from \'none\' to \'normal\'. Is there a way to give it focus as well so that when I click somewhere else o

11条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-05 09:00

    On triggering mouseup() event, we can check the click is inside the div or a descendant and take action accordingly.

    $(document).mouseup(function (e) {
        var divContent= $(".className");
        if(!divContent.is(e.target) && divContent.has(e.target).length === 0) {
            $(".className").hide();
        }
    });
    

提交回复
热议问题