JavaScript - 在手机上点击外部时隐藏元素 phone

JavaScript - hide element when click outside not working on mobile phone

隐藏模式 windows 使用此代码在台式计算机上工作正常

$(document).mouseup(function (e){
    if ($("#full_window_dim").is(':visible') && !$("#hodnotenie_hl_okno").is(e.target) && $("#hodnotenie_hl_okno").has(e.target).length === 0){
        $("#full_window_dim").fadeOut();
        $('html, body').removeClass('stop-scrolling');
}
});

但此代码不适用于手机(我已尝试使用 iPhone 5 - Safari)

当用户在手机上点击 window 之外时,如何隐藏该元素?

您可以在手机上使用 touchstart,因为大多数设备不理解鼠标事件。

这是代码。您可以添加任意数量的事件。

function HideModal(){
    if ($("#full_window_dim").is(':visible') && !$("#hodnotenie_hl_okno").is(e.target) && $("#hodnotenie_hl_okno").has(e.target).length === 0){
        $("#full_window_dim").fadeOut();
        $('html, body').removeClass('stop-scrolling');
    }
}

 $(document).on({
        "touchstart": function (event) { HideModal(); },
        "mouseup": function (event) { HideModal(); }
});

您可以为您的移动设备 "body{cursor:pointer}" 定义 css,然后 Safari Mobile 的行为为点击,而不是点击。但我不确定这是否是最佳方式。我刚刚试过了,对我有用。