开发者

Error with "this" in IE with jQuery plugin

开发者 https://www.devze.com 2023-04-09 20:48 出处:网络
I\'m coding a plugin for jQuery which now works fine on every browser but IE. This is part of the code:

I'm coding a plugin for jQuery which now works fine on every browser but IE. This is part of the code:

(function( $ ){
    $.fn.myPlugin = function(options) {

        var methods = {
            getFirstList: functio开发者_开发技巧n(el){
                return $("ul:first", el);
            }
        };

        return this.each(function(){
            ...
            var list = methods.getFirstList(this); 
            // "this" here refers to window or document in IE.
            ...
        });
    };
})( jQuery );

When I call the plugin ($("#myObject").myPlugin();), the keyword "this" is not refered to the DOM object, but to the window or document.

How do I fix this?


Try replacing this with $(this)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号