【发布时间】:2012-01-12 09:56:09
【问题描述】:
我正在编写一个 jquery 插件,用于在单击链接时显示一个 jquery ui 对话框,以便在链接之前提供一个确认对话框。
我遇到的问题是,当使用“是”按钮关闭对话框时,插件使用$(element).trigger( 'click' ); 在原始锚元素上触发点击事件。
这不会导致浏览器跟随链接,但是在对话框关闭后用鼠标再次单击不会工作。
插件是这样使用的$('a').submitConfirm();
这是插件
;(function ( $, window, document, undefined )
{
var pluginName = "submitConfirm";
var Plugin = function( element )
{
var confirmed = false;
var dialog = $( '<div style="display:none;">' )
.html( 'Visit this link?' )
.dialog(
{
modal: true,
title: 'Visit Link?',
autoOpen: false,
buttons :
[
{
text: 'Yes',
click: function( event )
{
confirmed = true;
dialog.dialog( "close" );
$(element).trigger( 'click' );
}
},
{
text: 'No',
click: function( event )
{
confirmed = false;
dialog.dialog( "close" );
}
}
]
});
$(element).bind( 'click',
function( event )
{
if ( ! confirmed )
{
dialog.dialog( "open" );
event.preventDefault();
}
});
};
// Init the plugin
$.fn[pluginName] = function( options )
{
return this.each(function ()
{
// Prevent re-instantiation
if ( !$.data(this, 'plugin_' + pluginName) )
{
$.data(this, 'plugin_' + pluginName,
new Plugin( this, options ));
}
});
};
})( jQuery );
【问题讨论】:
标签: javascript jquery jquery-plugins javascript-framework