سؤال

I have a URL that holds a "rel" attribute that needs to be passed to a jQuery function.

Here is the URL:

<a href="http://www.facebook.com/<?php echo $fblikeId; ?>" rel="<?php echo $fblikeId; ?>" ></a>

In a jQuery function I need to retrieve the value of the "rel" tag.

Here is the jQuery function:

jQuery(function($) {    
    $('#fblikes').advancedsocialstatus({
        'displayId':'fblikes',
        'service':'facebook',
        'countof':'likes',
        'userid': $(this).attr('rel'),
        'callback':'formatCount'
    });
});

The 'userid' parameter of the above function needs to retrieve the value of the "rel" tag. This above function then passes all that data onto a higher jQuery function which works fine.

What is the best way to retrieve the "rel" attribute of the link? Have tried using "$(this).attr('rel')" but doesn't work.

The above function has multiple nodes. IE There may be multiple calls

jQuery(function($) { 
    $('#fblikes').advancedsocialstatus({
        'displayId':'fblikes',
        'service':'facebook',
        'countof':'likes',
        'userid':$(this).attr('rel'),
        'callback':'formatCount'
    });
    $('#fbtalks').advancedsocialstatus({
        'service':'facebook',
        'countof':'talks',
        'userid':'$(this).attr('rel')',
        'callback':'formatCount'
    });

});
هل كانت مفيدة؟

المحلول

You can't use $(this) arbitrarily in the options object of a plugin function. However you already know the element so there is no need for this; just use the same selector.

var $fblikes=$('#fblikes');

$fblikes.advancedsocialstatus({
        'displayId':'fblikes',
        'service':'facebook',
        'countof':'likes',
        'userid': $fblikes.attr('rel'),
        'callback':'formatCount'
});

This code assumes that $('#fblikes') is for the <a> tag shown but there is no ID in that html

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top