javascript中类似警报的函数

alert like function in javascript

本文关键字:函数 javascript      更新时间:2023-09-26

我想用javascript创建一个模式弹出窗口
但我的要求是,每当弹出窗口打开时,它都会停止执行下一行javascript,直到关闭为止。

例如

var bool = false;
DisplayModal("Show Message");
bool = true;
alert("alerted only after closing the pop up : bool = true");
bool = false;

因此,在我关闭"Show Message"布尔的模式弹出窗口之前,它不会是真的。当我关闭弹出窗口时,剩余的脚本将在DisplayModal之后按顺序运行。。。

类似于alert,但我不想覆盖alert。。

我需要一些帮助,提前谢谢。。。

编辑:

试着多解释一下。。

DisplayModal调用
时,我想停止执行停止执行
之后会显示弹出窗口
弹出窗口关闭后,从下一行继续(表示:bool = true; ...

如果您想自定义对话框的外观,可以使用jQuery的对话框小部件。它支持事件。我举了一个简单的例子:http://jsfiddle.net/5CBdm/

您可以将事件处理程序附加到对话框的关闭事件,并将其余代码放入事件处理程序中。

所需的HTML:

<div id="dialog-modal" title="Modal dialog">
    <p>Message</p>
</div>

以及相应的Javascript:

$("#dialog-modal").dialog({
    height: 140,
    modal: true,
    close: function (event, ui) {
        alert("Executed when the dialog is closed.");
    }    
});

对话框链接:

http://jqueryui.com/dialog/#modal

http://api.jqueryui.com/dialog/#event-关闭

alert、prompt、confirm和XMLHTTPRequest是JavaScript中唯一的同步函数。您必须使用事件和回调。

如果你希望用户在关闭弹出窗口之前不能做任何事情,你可以制作一个透明的div来恢复所有页面(除了弹出窗口…)。

您可以使用jQuery插件。例如http://dimsemenov.com/plugins/magnific-popup/

或者你可以自己制作(在jsfiddle上试试:http://jsfiddle.net/T2Vn3/):

<div class="overlay">
    <div class="alert">
        This is alert!
        <br/>
        <button>OK</button>
    </div>
</div>
<button class="forAlert">Click me!</button>

JS:

document.querySelector('.alert button').addEventListener('click', function () {
    document.querySelector('.overlay').style.display = 'none';
}, false);
document.querySelector('.forAlert').addEventListener('click', function () {
    document.querySelector('.overlay').style.display = 'block';
});

听起来像是confirm 的工作

(function() {
var result = confirm("I'm Happy?");
alert(result ? "Yes, I'm happy" : "No, I'm not happy");
}());

Confirm将阻塞当前执行线程,直到返回结果