0
我不知道如何在fadeOut完成後訪問匿名函數中該特定實例的插件「選項」。訪問對象實例變量在匿名函數中的作用域
在匿名函數 '這個' 代表了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>
感謝