1
我想写一个基本的对话框。我希望能够指定一个链接元素,它将在点击时启动一个对话框。触发并绑定到自定义事件?
I'n使用数据指定此行为属性对话框中HTML:
<a id="launch-dialog" href="#"></a>
<div class="dialog" data-behavior="dialog" data-trigger="#launch-dialog">
<!-- dialog html -->
</div>
$(function(){
$("[data-behavior='dialog']").dialog();
});
实际jQuery的扩展名是我遇到麻烦,虽然部分。 'dopen'
事件未正确触发,或者事件绑定未正确设置。尽管如此,data-trigger
上的点击事件肯定是正在触发并正在收听。任何想法,为什么我从来没有看到“公开检测”日志?
(function($) {
var methods = {
init: function(options) {
if (this.data('trigger')) {
// Add an event listener which causes the dialog to
// open when the correct link is clicked.
$(this.data('trigger')).on('click', function(e) {
e.preventDefault();
// Trigger a global dialog open event which the dialog can subscribe to.
$('body').trigger('dopen');
});
}
},
// Various other methods not shown.
};
$.fn.dialog = function(method) {
// Method handling code not shown...
// Add handlers for custom events triggered on the body element.
$('body').bind('dopen', function(e) {
// This never happns:
console.log("Open detected");
});
};
})(jQuery);