写一个jquery插件在新的window中打开一个link

Write a jquery plugin to open a link in new window

我写了这个插件来打开 link 到新的 window 但是没有用 知道哪里出了问题吗?

(function( $ ) {
     $.fn.myPlugin = function() {
        var defaults = {
            width: 800,
            height: 700,
            scrollbars: 1,
            location: 1,
            status: 1       
        },
        self = this,
        opts = $.extend(defaults, options);
        this.filter('a').click(function() {
            $("a").attr("target","_self");
            window.open($(this).attr('href'),'title', opts);
            return this;
        });
    };

}( jQuery )); 
$('a').myPlugin();

我认为您缺少扩展函数中的选项声明变量,第二个参数尚未定义。

(function( $ ) {
     $.fn.myPlugin = function() {
        var defaults = {
            width: 800,
            height: 700,
            scrollbars: 1,
            location: 1,
            status: 1       
        },
        options,   //<-------- This one right here !
        self = this,
        opts = $.extend(defaults, options);
        this.filter('a').click(function() {
            $("a").attr("target","_self");
            window.open($(this).attr('href'),'title', opts);
            return this;
        });
    };

}( jQuery )); 
$('a').myPlugin();

祝其余代码顺利。

狮子座。

而不是这个

   opts = $.extend(defaults, options);
   this.filter('a').click(function() {
        $("a").attr("target","_self");
        window.open($(this).attr('href'),'title', opts);
        return this;
    });

尝试:

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

$('a').click(function(){
   window.open($(this).attr('href'),'title', opts);
});

Here you can find examples of using window.open(). Also read doc:

Keep in mind that the target object (first argument) will be modified, and will also be returned from $.extend(). If, however, you want to preserve both of the original objects, you can do so by passing an empty object as the target:

var object = $.extend({}, object1, object2);

希望对您有所帮助。