如何在 Pnotify 中设置按钮
How to set foucs on button in Pnotify
我在我的项目中使用 pnotify alert jquery。 我正在尝试在弹出对话框时将焦点设置为"确定"按钮。 这样用户只需按 Enter 或空格键即可关闭对话框。 但无法做到这一点。
这是通知的链接
我的代码 -
function AlertAskOk(Heading, Message, type, okclick) {
var modal_overlay;
info_box = $.pnotify({
title: Heading,
text: Message,
type: type,
buttons: 'ok',
okclick: okclick,
icon: "picon picon-object-order-raise",
delay: 20000,
history: false,
stack: false,
// nonblock: true,
before_open: function (pnotify) {
// $("btn-inverse").focus();
// Position this notice in the center of the screen.
pnotify.css({
"top": ($(window).height() / 2) - (pnotify.height() / 2),
"left": ($(window).width() / 2) - (pnotify.width() / 2)
});
// Make a modal screen overlay.
modal_overlay = $("<div />", {
"class": "ui-widget-overlay",
"css": {
"display": "none",
"position": "fixed",
"top": "0",
"width": "5000px",
"bottom": "0",
"right": "0",
"left": "0",
"cursor": "pointer"
}
}).appendTo("body").fadeIn("fast");
},
//....
after_open: function (ui) {
$(".btn", ui.container).focus();
},
//....
before_close: function () {
modal_overlay.fadeOut("fast");
}
});
}
使用after_open
回调。查看此演示。
new PNotify({
//....
after_open: function (notify) {
$(".btn-class", notify.container).focus();
}
//....
});
如果需要为所有PNotify更改此设置,我使用下一个解决方案:
PNotify.prototype.options.confirm.buttons[0].addClass = 'btn-pnotify-ok';
PNotify.prototype.modules.confirm.afterOpen = function(notice, options){
if (options.prompt) {
this.prompt.focus();
} else {
notice.container
.keyup(({keyCode}) => {
if (keyCode === 27) {
notice.remove();
}
})
.find('.btn-pnotify-ok')
.focus();
}
};
new PNotify({...});
...
new PNotify({...});
演示
PNotify.prototype.options.styling = 'bootstrap3';
PNotify.prototype.options.confirm.buttons[0].addClass = 'btn-pnotify-ok';
PNotify.prototype.modules.confirm.afterOpen = function(notice, options){
if (options.prompt) {
this.prompt.focus();
} else {
notice.container
.keyup(({keyCode}) => {
if (keyCode === 27) {
notice.remove();
}
})
.find('.btn-pnotify-ok')
.focus();
}
};
$("#btn1").click(function () {
new PNotify({
title: 'Focus on open #1',
text: 'Press [enter] or [esc]!',
hide: false,
stack: {
'modal': true,
'dir1': 'down',
'dir2': 'right',
},
confirm: {
confirm: true,
},
buttons: {
closer: false,
sticker: false
},
});
});
$("#btn2").click(function () {
new PNotify({
title: 'Focus on open #2',
text: 'Press [enter] or [esc]!',
hide: false,
stack: {
'modal': true,
'dir1': 'down',
'dir2': 'right',
},
confirm: {
confirm: true,
},
buttons: {
closer: false,
sticker: false
},
});
});
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/pnotify/3.2.1/pnotify.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/pnotify/3.2.1/pnotify.confirm.js"></script>
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.css">
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/pnotify/3.2.1/pnotify.css">
<button id="btn1" class="btn btn-default">Confirm Dialog #1</button>
<button id="btn2" class="btn btn-default">Confirm Dialog #2</button>
相关文章:
- 如何在react js中设置按钮点击的值
- 在jquerymobile中设置按钮css属性的动画
- 如何使用Jquery设置按钮样式
- 如何设置按钮来更改按钮的背景色
- 如何在表单中设置按钮,在提交时检查所需字段,但不刷新页面
- 如何在 Pnotify 中设置按钮
- 如何通过带有 onclick 事件的开关设置按钮值
- 通过 javascript 设置按钮属性
- 在 Ajax 工具包 htmleditorextender 中设置按钮单击事件的文本
- 如何设置按钮以使用 jQuery、AJAX、JSON 和 MVC 调用不同的按钮
- 如果在 ng-repeat中至少选中了一个复选框,则设置按钮的验证
- jQuery UI-dialog-动态设置按钮并将值传递给函数
- 使用原型设置按钮的 onclick 事件
- 以编程方式设置按钮文本
- 使用javascript设置按钮的不透明度值
- 设置“n”按钮的 ID
- 我怎么能说"如果#菜单的显示是块然后设置#按钮的显示是块"在js和jquery中
- 如何动态设置按钮id并在其上绑定点击事件
- 我无法设置按钮的数据属性
- 在ExtJS中设置按钮(或菜单)的样式