jQuery - How can I do "per element attribute evaluation"
I've got a tweaked version of the jQuery TimeAgo plugin.. the only tweaking is the fact that I can set custom attributes instead of global ones. The reason for this is so that I can have a suffix on some tags, and no suffix on others. This tweaking does work perfectly on it's own as I have tested it in different ways.
However, the way I'd like to select the elements/attributes is not working properly.
My markup has the following
<time data-suffixAgo="" datetime="2010-06-24T14:10:22Z" title="2010-06-24T14:10:22Z">开发者_JAVA百科4 months </time>
<time data-suffixAgo="ago" datetime="2010-11-10T06:00:44Z" title="2010-11-10T06:00:44Z">less than a minute ago</time>
<!-- note the data between the <title> tags is generated server side.
I'm not talking about that data, just the jQuery information-->
And the output at runtime says
4 months ago
less than a minute ago
now the problem with this is that the "4 months ago" is not supposed to say "ago" because my data-suffixAgo
attribute is blank
Here is the jQuery I'm using
// selects all "time" tags on the page and give the friendly "timeago"
// uses the jquery.timeago.js plugin
// all time tags are created using the "ToTimeSpan" Date Extension Method
$('time').timeago({strings:{suffixAgo: $(this).attr("data-suffixAgo")}});
Can anyone tell my why my selector is not pulling the data-suffixAgo
attribute on a per element basis?
If I use this
$('time').timeago({strings:{suffixAgo: ""}});
the "ago" will be removed from all <time>
tags, and if I do this
$('time').timeago({strings:{suffixAgo: "ago"}});
the "ago" will be added to all <time>
tags
Try:
$('time').each(function(){
$(this).timeago({
strings: {suffixAgo: $(this).attr("data-suffixAgo") }
});
});
The $(this) you have in your code refers to the whole set of $('time').
精彩评论