开发者

jquery: Add class if link has a target blank?

hey guys, shouldn't that work?

//Add class if target blank
    $('.post .entry a').each(function() {
        if ( $(this).attr('target') == '_blank') ) {
            $(this).ad开发者_开发百科dClass('web');
        };
    });

anything wrong with this?


Please try

//Add class if target blank
$('.post .entry a[target="_blank"]').addClass('web');


You have an extra ). Just remove that and it's fine :)

$('.post .entry a').each(function() {
    if ( $(this).attr('target') == '_blank') ) {
---------------------------------------------^
        $(this).addClass('web');
    };
});


It's fine. But this should be inside a document.ready:

$(document).ready(function(){
   $('.post .entry a').each(function() {
        if ( $(this).attr('target') == '_blank') ) {
            $(this).addClass('web');
        };
    });
});

Hope this helps. Cheers


You have am extra ) on the if statement. Take that off an you'll be ok:

$('.post .entry a').each(function() {
    if ( $(this).attr('target') == '_blank' ) {
        $(this).addClass('web');
    };
});

A shorter way to do this would be:

$('.post .entry a[target="_blank"]').each(function() {
    $(this).addClass('web');
});

Uses less code too.

Actually @ariel's answer states a better way to do it.


I found success with the following:

$('.post .entry a[href="_blank"]').addClass('web');
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜