强制元素在文本框外单击时隐藏

Force element to hide on click outside of textbox

这是我的资料:

$(document).ready(function () {
$('#H-Alert').hide();

$('html').click(function (e) {
    if (e.target.className == '.h-textbox') {
        $('#H-Alert').show();
    }
    else {
        $('#H-Alert').hide();
    }
});
});

我参考了这个:

Reference

感谢任何帮助。

您可能正在寻找 blur and focus 事件。您可以使用 jQuery 执行此操作,如下所示:

$('#H-Alert').hide();
$(window).ready(function(){
    $('.h-textbox').blur(function(){
      $('#H-Alert').show();
    }).focus(function(){
      $('#H-Alert').hide();
    });
});

对于进入或离开文本框的事件,@meltuhamy 的解决方案可能更好,但如果你想直接处理这些事件,你可以使用这个:

$(document).click(function(event) {
    if($(event.target).closest(".h-textbox").length == 0) {
        if($("#H-Alert").is(":visible")) {
            $("#H-Alert").hide();
        } else {
            $("#H-Alert").show();
        }
    }
});