【发布时间】:2009-10-20 09:19:46
【问题描述】:
目前我正在编写一个带有一些选项的 jQuery 插件。
网页中的一段简化代码示例:
<div id="div1"></div>
<div id="div2"></div>
$(document).ready(function(){
$("#div1").myFunc({width: 100, height: 100});
$("#div2").myFunc({width: 200, height: 200});
});
这是一个(再次简化的)插件代码:
(function($) {
$.fn.myFunc = function(options) {
// extending default settings
var options = $.extend( {
width: 300,
height: 200
}, options);
return this.each(function() {
// doing something for example with #div1
$(this).click(function() {
// here I need to access ANOTHER (e.g. #div2) object's options
// how can I do it?
});
});
}
})(jQuery);
好吧,问题就在清单中 - 我如何从插件函数内部访问另一个对象的选项?类似 $("#div2").options.width
【问题讨论】:
-
对不起 - 应该在第一个列表中写 $("#div1"), $("#div2")
-
我认为您的 return 语句也需要在插件的函数范围内。目前,它在它之外(但在自调用匿名函数的函数范围内)。你想编辑你的问题代码吗?
-
哦,是的,谢谢 - 已更正
标签: jquery jquery-plugins