【发布时间】:2021-11-01 00:11:21
【问题描述】:
我正在尝试将 toggle 按钮和 $('#popup').blur 保持在一起,但不幸的是触发器停止隐藏。 我该如何解决这个问题?
我在标题中有这个触发器
$(document).ready(function() {
$('#toggle').click(function() {
// this alert always returns 'none'
alert($('#popup').css('display'));
if ($('#popup').css('display') == 'none') {
$('#popup').show().focus();
} else {
$('#popup').hide();
}
});
$('#popup').blur(function() {
$(this).hide();
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<div class="trigger" id="toggle">
<i class="las la-bell"></i>
</div>
当我检查显示时,尽管有 阻止,此警报仍返回 none。
alert($('#popup').css('display'));
为什么会这样?首先,下面的代码无论如何都应该隐藏我的弹出窗口(无论我点击哪个元素),问题是弹出窗口在返回显示时没有隐藏:none
$('#popup').blur(function() {
$(this).hide();
});
有我的代码:https://codepen.io/webtm/pen/xxrOwMb
我尝试了一些资源,但没有解决方案。 Toggle a popup and toggle it when clicked outside
当我使用这个例子时也会发生同样的情况
$(document).mouseup(function(e)
{
var container = $("YOUR CONTAINER SELECTOR");
// if the target of the click isn't the container nor a descendant of the container
if (!container.is(e.target) && container.has(e.target).length === 0)
{
container.hide();
}
});
Use jQuery to hide a DIV when the user clicks outside of it
你有什么想法吗?
【问题讨论】:
-
我给你做了一个sn-p。请添加相关的 HTML,例如 bootstrap css
-
您的问题是您的调试
alert将焦点从文档中移开 - 删除警报并且您的弹出模糊不会触发并且它 works fine -
您好,我不太明白您的问题。但正如我在您的 codepen 中看到的那样,当您发出警报时 - 然后您需要 单击确定以关闭警报(这意味着 警报的按钮现在获得焦点并且你的弹出窗口失去焦点 - 所以它被隐藏了)。
标签: jquery focus toggle display blur