我已经使用 jQuery 好几年了,我很喜欢它。但是,我的项目中有很多函数,我想将它们作为扩展的 jQuery 函数。
对于这个例子,我将使用 $.post 函数来演示当我尝试使用我自己的函数来完成时:
$.post('file.php', { arg1: 'hi', arg2: 'hi2' }, function ( data ) {
// Is called after function completes
// and returns a value (in this case data)
});
我认为该功能的外观
(function( $ ) {
$.fn.popup = function( options ) {
options = $.extend( $.fn.popup.defaults, options );
if ( options.type === 1 )
var opt = '<table><tr><td><div id="pConfirm">CONFIRM</div></td><td><div id="pCancel">CANCEL</div></td></tr></table>';
$('body').prepend('<div class="overlay-control"></div>');
$('body').prepend('<div class="info-overlay"><h1>'+title+'</h1>'+ message + opt +'</div>');
$(document).on('click', 'pConfirm', function () {
//callback would happen here and would return a value
});
$(document).on('click', 'pCancel', function () {
//callback would happen here and would return a value
});
});
怎么称呼
$.popup('title', 'message', 'type', function(ret) {
if ( ret === 0 )
//Cancelled
else
//Accepted
}
我希望有人可以发布一个支持一些值并能够返回结果或将我指向一个好的教程的示例。
干杯!