2014-03-29 44 views
0

这是我的插件代码如下。

$.fn.myplugin = function(options) { 

    var defaults = { 
     my_data_title11:"", 
     my_div:".getslow", 
    } 

    var settings = $.extend({}, defaults, options); 

    return this.each(function(){ 
     console.log(options.my_data_title); 
    }); 
} 

我打电话从这样一个外部页面此插件...

$('#o_vari,#0_var2').myplugin({ 
    my_data_title11:$(this).attr("id"),my_div:'.getfast' 
}); 

,但它显示undefined。我知道my_data_title11:$(this).attr("id")是不承认$(this)调用声明,我甚至尝试把这个外部变量,但仍然是同样的问题。

+0

您是否尝试过使用'。每()'? –

回答

1

从你写的代码的方式,我假设你想打印o_vari0_var2 ......但事实并非如此,因为在你已经使用$(this).att('id')的背景下,this不指o_varx元素。

所以,如果你想所需的输出尝试

$('#o_vari,#0_var2').each(function() { 
    $(this).myplugin({ 
     my_data_title: this.id, 
     my_div: '.getfast' 
    }); 
}) 

演示:Fiddle

+0

完美答案:)接受 – Friend

+0

[如何接受答案](http://meta.stackexchange.com/a/5235/147626) –