【发布时间】:2012-11-20 04:16:02
【问题描述】:
我是 JavaScript 和 Jquery 的新手。我在网上搜索了 Jquery 弹出示例。我希望消息说“我们的网站尚未完成,但请随意浏览我们拥有的内容。”我将包含我找到的代码示例,但它看起来很奇怪。我不确定函数的名称是什么以及如何使用 window.onload = function(); 执行它代码。我还想要一个关闭文本框的“关闭”按钮。这是它的样子:http://demo.tutorialzine.com/2010/12/better-confirm-box-jquery-css3/
这是第一部分:
(function($){
$.confirm = function(params){
if($('#confirmOverlay').length){
// A confirm is already shown on the page:
return false;
}
var buttonHTML = '';
$.each(params.buttons,function(name,obj){
// Generating the markup for the buttons:
buttonHTML += '<a href="#" class="button '+obj['class']+'">'+name+'<span></span></a>';
if(!obj.action){
obj.action = function(){};
}
});
var markup = [
'<div id="confirmOverlay">',
'<div id="confirmBox">',
'<h1>',params.title,'</h1>',
'<p>',params.message,'</p>',
'<div id="confirmButtons">',
buttonHTML,
'</div></div></div>'
].join('');
$(markup).hide().appendTo('body').fadeIn();
var buttons = $('#confirmBox .button'),
i = 0;
$.each(params.buttons,function(name,obj){
buttons.eq(i++).click(function(){
// Calling the action attribute when a
// click occurs, and hiding the confirm.
obj.action();
$.confirm.hide();
return false;
});
});
}
$.confirm.hide = function(){
$('#confirmOverlay').fadeOut(function(){
$(this).remove();
});
}
})(jQuery);
这是第二部分:
$(document).ready(function(){
$('.item .delete').click(function(){
var elem = $(this).closest('.item');
$.confirm({
'title' : 'Delete Confirmation',
'message' : 'You are about to delete this item. <br />It cannot be restored at a later time! Continue?',
'buttons' : {
'Yes' : {
'class' : 'blue',
'action': function(){
elem.slideUp();
}
},
'No' : {
'class' : 'gray',
'action': function(){} // Nothing to do in this case. You can as well omit the action property.
}
}
});
});
});
$(markup).hide().appendTo('body').fadeIn();
var buttons = $('#confirmBox .button'),
i = 0;
$.each(params.buttons,function(name,obj){
buttons.eq(i++).click(function(){
// Calling the action attribute when a
// click occurs, and hiding the confirm.
obj.action();
$.confirm.hide();
return false;
});
});
}
$.confirm.hide = function(){
$('#confirmOverlay').fadeOut(function(){
$(this).remove();
});
}
})(jQuery);
编辑:我收到了要在加载时显示的消息。我将第二部分中的代码更改为
$('.item .delete').ready(function(){
【问题讨论】:
-
你想要一个弹出窗口还是一个对话框。
-
什么意思?我希望它在页面加载时弹出。我不确定你所说的对话是什么意思。
-
在浏览器中弹出意味着应用程序打开一个新的浏览器实例并使用相同的会话信息。对话框意味着基于 Div 的确认框打开(就像警报一样)并且它是同一页面的一部分
-
我想我明白了你的意思,也看到了这个例子。
-
对话框,然后。我想要一些优雅的东西而不是传统的 JavaScript 警报。
标签: javascript jquery html css popup