fadeOut 完成后,我不知道如何在匿名函数中访问此特定实例的插件“选项”。
在匿名函数“this”代表 jquery 元素中,我如何访问“options.name”?
这是插件“plugin.js”:
(function ($, window, document, undefined) {
'use strict';
var plugin = 'box',
defaults = {
wight: 26,
color: 'white'
};
function Plugin(element, options) {
this.plugin = plugin;
this.element = element;
this.options = $.extend({}, defaults, options);
$(this.element).fadeOut(1000, function () {
console.log(this.options.name); // <- how do I access options.name?
});
}
Plugin.prototype = {
f: function () {
console.log(this.options.name);
}
};
$.fn[plugin] = function (argument) {
var method = argument,
options = argument;
return this.each(function () {
if (!$.data(this, 'plugin_' + plugin)) {
$.data(this, 'plugin_' + plugin, new Plugin(this, options));
} else if ($.isFunction(Plugin.prototype[method])) {
$.data(this, 'plugin_' + plugin)[method](Array.prototype.slice.call(arguments, 1));
} else {
console.error('unknown method ' + method);
}
});
};
}(jQuery, window, document));
这是'index.html':
<!DOCTYPE html>
<html class="no-overflow">
<head>
<meta charset="UTF-8">
<title>Table example</title>
<!-- jqwidgets styles -->
<style type="text/css">
</style>
<!-- jquery framework -->
<script type="text/javascript" src="../lib-scripts/jquery-1.10.2.min.js"></script>
<!-- script -->
<script type="text/javascript" src="plugin.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$('#id-a').box({ name: 'aaaa' });
$('#id-b').box({ name: 'bbbb' });
$('#id-a').box('f');
$('#id-b').box('f');
});
</script>
</head>
<body>
<div id="id-a"></div>
<div id="id-b"></div>
</body>
</html>
谢谢