2017-07-31 116 views
0

我想從html元素中獲取對象選項和函數。 我

$.fn.test = function(options){ 

    var me = this; 

    this.create = function(){ 

    me.settings = $.extend({ 
     foo: 'foo' 
    }, options); 

    console.log('foo'); 
    } 

    this.hello = function(){ 
    console.log('hello'); 
    } 

    return this; 

} 

在文件準備好創建我的對象

$(document).ready(function(){ 
    var obj = $('#mydiv').test({ 
    foo: 'bar'; 
    }); 
    obj.create(); 
}); 

在另一個文件中,我tryng從#mydiv像獲取設置:

$('form').on('submit', function(){ 
    var x = $('#mydiv').test(); 
    //x.create(); 
    console.log(x.settings.foo); 
}); 

如果在最後節我稱之爲創建功能,它顯示我'富',而不是'酒吧'。我能怎麼做?

回答

0
if(options) me.settings = $.extend({ 
    foo: 'foo' 
}, options); 

只是覆蓋.settings如果那裏有真正的選擇對象..

如果jQuery的內嵌功能鏈接,所以你可以做我不是舒爾:

this.hello = function(){ 
console.log('hello'); 
return this; 
} 
+0

感謝您的回答,但我必須在其他js文檔中獲取函數。它將我的初始值返回給我 – martina