Javascript 如何使用'$(本)及"x27 ;;在jquery插件参数中

Javascript 如何使用'$(本)及"x27 ;;在jquery插件参数中,javascript,jquery,jquery-plugins,Javascript,Jquery,Jquery Plugins,我正在编写一个jQuery插件,并试图在其中一个参数中使用$(this)选择器,但它尚未定义 这是我的密码: $('.foo').loadMore({ onScreen: function() { $(this).css('background-color', 'red'); } }) 下面是我的插件中的代码: $.fn.loadMore = function( options ) { var settings = $.extend({

我正在编写一个jQuery插件,并试图在其中一个参数中使用$(this)选择器,但它尚未定义

这是我的密码:

$('.foo').loadMore({
    onScreen: function() {
        $(this).css('background-color', 'red');
    }
})
下面是我的插件中的代码:

$.fn.loadMore = function( options ) {

    var settings = $.extend({
        onScreen: function(){}, 
    }, options);

    return this.each(function(index, ele) {
        settings.onScreen();
    });
};

您必须使用正确的
此值在屏幕上调用

settings.onScreen.call(this);
如果希望与内置jQuery方法更加一致,还可以传递索引和元素:

settings.onScreen.call(this, index, this);
参考: