При использовании rel external в jquery встроенный плагин открывает ссылку 2 раза - PullRequest
0 голосов
/ 19 июня 2010

Используя плагин jquery fit *, если я добавлю rel = "external" в интерактивную область, она будет успешно открыта в новой вкладке, но нажатие на ссылку откроется 2 раза, как это исправить?

например: у меня есть:

<p>
text <a rel="external" href="/link">text2</a>
</p>

текст открывается идеально один раз, но при нажатии на текст2 ссылка открывается два раза

код:

if(typeof jQuery != 'undefined') {
    jQuery(function($) {
        $.fn.extend({
            fitted: function(options) {
                var settings = $.extend({}, $.fn.fitted.defaults, options);

                return this.each(
                    function() {

                        var $t = $(this);
                        var o = $.metadata ? $.extend({}, settings, $t.metadata()) : settings;

                        if($t.find(':has(a)')) {
                            /**
                            * Find the first Anchor
                            * @var object $a
                            */
                            var $a = $t.find('a:first');

                            /**
                            * Get the Anchor Attributes
                            */
                            var href = $a.attr('href');
                            var title = $a.attr('title');

                            /**
                            * Setup the Container
                            * Add the 'container' class defined in settings
                            * @event hover
                            * @event click
                            */
                            $t.addClass(o['class']['container']).hover(
                                function(){
                                    /**
                                    * Hovered Element
                                    */
                                    $h = $(this);

                                    /**
                                    * Add the 'hover' class defined in settings
                                    */
                                    $h.addClass(o['class']['hover']);

                                    /**
                                    * Add the Title Attribute if the option is set, and it's not empty
                                    */
                                    if(typeof o['title'] != 'undefined' && o['title']===true && title != '') {
                                        $h.attr('title',title);
                                    }

                                    /**
                                    * Set the Status bar string if the option is set
                                    */
                                    if(typeof o['status'] != 'undefined' && o['status']===true) {
                                        if($.browser.safari) {
                                            /**
                                            * Safari Formatted Status bar string
                                            */
                                            window.status = 'Go to "' + href + '"';
                                        }
                                        else {
                                            /**
                                            * Default Formatted Status bar string
                                            */
                                            window.status = href;
                                        }
                                    }
                                },
                                function(){
                                    /**
                                    * "un"-hovered Element
                                    */
                                    $h = $(this);

                                    /**
                                    * Remove the Title Attribute if it was set by the Plugin
                                    */
                                    if(typeof o['title'] != 'undefined' && o['title']===true && title != '') {
                                        $h.removeAttr('title');
                                    }

                                    /**
                                    * Remove the 'hover' class defined in settings
                                    */
                                    $h.removeClass(o['class']['hover']);

                                    /**
                                    * Remove the Status bar string
                                    */
                                    window.status = '';
                                }
                            ).click(
                                function(){
                                    /**
                                    * Clicked!
                                    * The Container has been Clicked
                                    * Trigger the Anchor / Follow the Link
                                    */
                                    if($a.is('[rel*=external]')){
                                        window.open(href);
                                        return true;
                                    }
                                    else {
                                        //$a.click(); $a.trigger('click');
                                        window.location = href;
                                    }
                                }
                            );
                        }
                    }
                );
            }
        });

        /**
        * Plugin Defaults
        */
        $.fn.fitted.defaults = {
            'class' : {
                'container' : 'fitted',
                'hover' : 'hovered'
            },
            'title' : true,
            'status' : true
        };
    });
}

*: http://www.trovster.com/lab/plugins/fitted/

Как это решить? можно исправить?

Большое спасибо

Ответы [ 2 ]

1 голос
/ 11 октября 2010

Решено:

это:

 if($a.is('[rel*=external]')){
            window.open(href);
            return true;
            }

для этого:

 if($a.is('[rel*=external]')){
            window.open(href);
            return false;
            }
0 голосов
/ 11 октября 2010

Я думаю, что на самом деле, чтобы иметь возможность использовать другие функции на кликах, лучше сделать это:

.click(function(e){
  e.preventDefault();
  if($a.is('[rel*=external]')){
    window.open(href);
  } else {
    window.location = href;
  }
});
...