[英]How to close an alert box when clicking on it?
Hey guys and girls.嘿男孩和女孩。 Made this custom alert box.
制作了这个自定义警报框。
<script type="text/javascript">
$(function () {
var $alert = $('#alert');
if ($alert.length) {
var alerttimer = window.setTimeout(function () {
$alert.trigger('click');
});
$alert.animate({ height: $alert.css('line-height') || '80px' }, 200).click(function () {
window.clearTimeout(alerttimer);
$alert.animate({ height: '0' }, 200);
});
}
});
</script>
I want it to be open until the user chooses to click on it or anywhere else on the screen.我希望它一直打开,直到用户选择单击它或屏幕上的任何其他位置。 Who do I make this happen?
我让谁来做这件事?
Thanx for your help!感谢您的帮助!
Assuming that clicking anywhere (in, or out, of the alert
box itself) is supposed to hide/remove the alert:假设单击任何位置(
alert
框本身的内部或外部)应该隐藏/删除警报:
$('body').click(
function(){
if ($alert.is(':visible')){
$alert.hide();
}
});
should work, I think.应该工作,我想。
If you want to justs get rid of it, try calling the hide() function when an onclick event is triggered.如果您想摆脱它,请尝试在触发 onclick 事件时调用 hide() function。
$.click(function() {
$alert.hide();
});
The top answer here is excellent IMO. 这里的最佳答案是优秀的 IMO。 It covers the part about anywhere else on the screen .
它涵盖了屏幕上其他任何地方的部分。
The way to do it is to bind a click
event to the body and stop propagation on any event that happens inside the area that isn't supposed to trigger closing the alert.这样做的方法是将
click
事件绑定到正文,并停止在不应触发关闭警报的区域内发生的任何事件的传播。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.