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);