重写 JavascriptConfirm() 同时保留回调
到目前为止,我想重写 jQuery 插件中的标准 js confirm()
函数。我已经弄清楚如何使用下面的简单功能布局来做到这一点。
function confirm(opts) {
//code
}
现在,我想做的是在上面的 confirm
函数中调用另一个函数,就像这样...
function confirm(opts) {
openModal(opts);
}
openModal
函数将打开一个自定义模式窗口,其中包含消息和所需的信息按钮。这两个按钮都是 ,
id
为 submit
或 cancel
。 submit
返回 true,cancel
返回 false。但现在,我如何根据单击的按钮返回 true 或 false?
例如......
$('#openModal').live('click', function() {
var opts = { title:'Modal Title', message:'This is a modal!' };
var resp = confirm(opts);
if(resp)
// user clicked <span id="submit"></span>
else
// user clicked <span id="cancel"></span>
});
希望你明白我的意思。
To the point, I want to override the standard js confirm()
function within a jQuery plugin. I have figured out how to do it with the simple function layout below.
function confirm(opts) {
//code
}
Now, what I want to do is call another function within the confirm
function above, like so...
function confirm(opts) {
openModal(opts);
}
The openModal
function will open a custom modal window with the message and required buttons. The buttons are both <span>
with the id
of either submit
or cancel
. submit
returns true and cancel
returns false. But now, how do I return either true or false based on the button clicked?
For example...
$('#openModal').live('click', function() {
var opts = { title:'Modal Title', message:'This is a modal!' };
var resp = confirm(opts);
if(resp)
// user clicked <span id="submit"></span>
else
// user clicked <span id="cancel"></span>
});
Hopefully you understand what I mean.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是可能的,但不完全像您所描述的那样。确认函数的典型使用方式是:
在上面的示例中,按钮按下同步返回到脚本。函数内的执行会暂停,直到按下按钮为止。
“模态对话框”只是 HTML 网页全部内容的覆盖层。对于浏览器来说,此类覆盖层中的按钮的工作方式与该页面中其他任何位置的按钮的工作方式相同。
由于 JavaScript 异步处理此类按钮单击(即使用回调函数来处理事件)而不是同步,因此您尝试的方法将不起作用。
您需要将使用确认函数的代码的所有部分更改为如下所示:
这就像使用 jQuery 本身注册事件处理程序一样工作。脚本立即继续执行,跳过匿名函数中的所有代码。稍后,浏览器将通过您在确认函数中注册的 JavaScript 事件处理程序间接调用该函数,其目的是根据需要使用
true
或false
调用回调函数。This is possible but not exactly as you describe. The typical way the confirm function is used is:
In the above example, the button press is returned synchronously to the script. Execution is paused within the function until a button is pressed.
A "modal dialog" is just an overlay over the entire contents of an HTML web page. To the browser, buttons within such an overlay work the same way as buttons anywhere else within that page do.
Since JavaScript handles such button clicks asynchronously (i.e. using callback functions to handle events) rather than synchronously, the approach you are attempting will not work.
You would need to change all parts of the code that use the confirm function to look like this:
This would work just as registering an event handler with jQuery itself does. Script execution continues immediately, skipping past all the code in the anonymous function. Later, the browser will call that function indirectly through a JavaScript event handler that you register within the confirm function, its purpose to call the callback function with either
true
orfalse
as appropriate.