简体   繁体   English

像JavaScript中的警报功能

[英]alert like function in javascript

I want to create a modal popup in javascript. 我想在javascript中创建模式弹出窗口。
But my requirement is whenever the popup open, it will stop executing next javascript lines until it is closed. 但是我的要求是,每当弹出窗口打开时,它将停止执行下一个javascript行,直到关闭为止。

for eg. 例如

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

So until I close the modal popup of "Show Message" bool would not be true. 因此,直到我关闭"Show Message"的模式弹出框,布尔值才会为真。 and when I close popup then remaining script will be run as in order after DisplayModal ... 当我关闭弹出窗口时,其余的脚本将在DisplayModal之后按顺序运行...

Similarly like alert but I do not want to override alert .. 同样,就像alert但我不想覆盖alert ..

I need some help, Thanks in advance... 我需要一些帮助,在此先感谢...

Edit : 编辑:

Try to explain a bit more.. 尝试解释更多..

I want to stop execution when DisplayModal called 我想在DisplayModal调用时停止执行
Stop execution 停止执行
after then Pop-up shows 之后弹出窗口显示
after the pop-up closed continue from next line (means : bool = true; ... ) 弹出窗口关闭后,从下一行继续(意味着: bool = true; ...

If you want to customize the appearance of the dialog, you can use jQuery's dialog widget. 如果要自定义对话框的外观,可以使用jQuery的对话框小部件。 It supports events. 它支持事件。 I've made a simple example: http://jsfiddle.net/5CBdm/ 我举了一个简单的例子: http : //jsfiddle.net/5CBdm/

You can attach an event handler to the close event of the dialog and put the rest of the code inside the event handler. 您可以将事件处理程序附加到对话框的close事件,并将其余代码放入事件处理程序中。

The required HTML: 所需的HTML:

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

And the corresponding Javascript: 以及相应的Javascript:

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

Links to the dialog: 链接到对话框:

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

http://api.jqueryui.com/dialog/#event-close http://api.jqueryui.com/dialog/#event-close

You can use jQuery plugin. 您可以使用jQuery插件。 For example http://dimsemenov.com/plugins/magnific-popup/ 例如http://dimsemenov.com/plugins/magnific-popup/

Or you can make it by yourself (try it on jsfiddle: http://jsfiddle.net/T2Vn3/ ): 或者,您也可以自己制作(在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: 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';
});

Sounds like a job for confirm 听起来像是需要confirm的工作

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

Confirm will block the current thread of execution until a result is returned 确认将阻止当前执行线程,直到返回结果

alert, prompt, confirm and XMLHTTPRequest are the only synchronous functions in JavaScript. alert,提示,确认和XMLHTTPRequest是JavaScript中唯一的同步功能。 You have to use events and callbacks. 您必须使用事件和回调。

If you want the user not to be able to do anything until he closes your popup, you can make a transparent div recovering all the page (except your popup ...). 如果您希望用户在关闭弹出窗口之前不能执行任何操作,则可以使透明div恢复所有页面(弹出窗口除外)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM