gpt4 book ai didi

jQuery 警报对话框

转载 作者:行者123 更新时间:2023-12-03 22:47:18 25 4
gpt4 key购买 nike

我对 jquery 很陌生,我一直在寻找可以替代确认对话框的东西。我在 http://abeautifulsite.net/blog/2008/12/jquery-alert-dialogs/#demo 找到了 jQuery 警报对话框但 jConfirm 不会返回与 inform() 相同的值。是否可以编写一个函数来获得相同的confirm()值?我承认我对回调函数的含义不太清楚:-)

最佳答案

jConfirm 永远不会“返回”任何内容,因为它是“事件驱动的”。

jConfirm 等待用户做出决定,然后它将调用处理答案的回调函数。 但是 jConfirm 不会像标准 confirm(...) 那样阻止代码执行流程。

因此,如果您以前的代码如下所示:

// ask for a decision
var answer = confirm("Leave website?"); // the whole script stops until the user has made a decision!

// process answer
if (answer){
alert("Bye bye!"); // the script waits here until the user has clicked "ok"
window.location = "http://www.google.com/";
}
else{
alert("Thanks for sticking around!"); // the script waits here until the user has clicked "ok"
}

它在 jQuery 中应该是这样的:

// previous code

// show confirm dialog but the script will not wait, the script execution goes forward

jConfirm('Leave website?', 'Confirmation Dialog', function(answer) {

// this is the callback function of the jConfirm dialog we made
// we arrive here when the user has made a decision

// the answer is true, he wants to leave
if (answer){

// show a jAlert box
jAlert("Bye Bye!", "Some Title", function() {

// this is the callback function of the jAlert box
// we arrive here when the user has clicked "ok"

// send him to google
window.location = "http://www.google.com/";
});

}
else{
// show a jAlert box without any callback
jAlert("Thanks for sticking around!", "Some Title");
}

});

// the code that follows here will be immediately executed
// because unlike confirm(), jConfirm() will not block
// the code execution flow

为了说明:

标准JavaScript的confirm()执行流程

  |
|
|
\/
confirm() waits for an answer...
no further code will be executed
until the user has made a decision
|
|
\/
handle the user's answer
|
|
\/
further code
execution

jConfirm 执行流程

  |
|
\/ -------> jConfirm Dialog
| |
| |
| \/
| Callback function
| when the user has made
| a decision.
| (handle the answer here)
|
|
\/
further code
execution

关于jQuery 警报对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6018946/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com