开发者

how can I tell jquery not to return true in a function if the a href has a specific class?

I am using "ajax for all" Wordpress plugin to asynchronously load content, but I cannot use any Lightbox plugin as after clicking the image the page reloads; I would like to override the function if the a href has a specific class

$('a').live( 'click',
        function() {
            if ( $(this).attr('target') )  {
                return true;
            }
            var href = $(this).attr('href');
            $.bbq.pushState({ url: href });
            if ( href.indexOf('#') == 0 ) {
                return true;
            }
            dispatcher.fire(href);
            return false;
        }
    );

How should I modify this function to ma开发者_如何学Cke it return false if the "a" is an image with a specific class?


You can use hasClass method to check if an element has a particular class.

Try this:

 $('a').live( 'click',      function() {
               //Check if the element has a class and if yes, return false.
        if($(this).hasClass("<YOUR_CLASS_NAME>")){
            return false;
        }
        else if ( $(this).attr('target') )  {
            return true;
        }
        var href = $(this).attr('href');
        $.bbq.pushState({ url: href });
        if ( href.indexOf('#') == 0 ) {
            return true;
        }
        dispatcher.fire(href);
        return false;
});


$('a').live( 'click', function( e ) { 
  e.preventDefault();
  if ( $(this).attr('target') ) { return true; } var href = $(this).attr('href'); $.bbq.pushState({ url: href }); if ( href.indexOf('#') == 0 ) { return true; } dispatcher.fire(href); return false; } );

This way you prevent the default link behavior. If you still need to do this only for a specified class link, just add a if statement

if ($(this).hasClass('myclass') {
  e.preventDefault();
}

Note you could also use return false; instead of e.preventDefault().

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜