I have a feed page that needs to have a click handler applied to every link in the rss feed..
I'm using ZRSSfeed which is great but it won't do callbacks so the .click() function runs long before the rss content arrives
there's a fiddle of this here:
http://jsfiddle.net/PMtCN/
开发者_运维百科thanks much
If I'm understanding the issue, you want to attach a click handler to the result, but don't know when the content is loaded. If this is the case, then simply use live:
Description: Attach a handler to the event for all elements which match the current selector, now and in the future.
http://jsfiddle.net/PMtCN/11/
jQuery('#linkwidget a').live("click",function(){
alert('Hello World');
return false;
});
The latest version has a callback function, so you can now perform actions on feed items after it has loaded.
The example below limits the feed title length to 14 characters:
$(document).ready(function () {
$('#test').rssfeed('http://feeds.reuters.com/reuters/oddlyEnoughNews',{},function(e) {
$('h4 a',e).each(function(i) {
var title = $(this).text();
if (title.length > 14) $(this).text(title.substring(0,14)+'...');
});
});
});
http://www.zazar.net/developers/jquery/zrssfeed
If you can't use the callback, and you want to individually apply the click function to each match node you can use code like this,
var COM_MYSITE_RSS_CHECK;
var COM_MYSITE_RSS_CHECK_COUNT = 0;
var COM_MYSITE_RSS_CHECK_FREQ = 100;
var COM_MYSITE_RSS_CHECK_MAX = 100;
function wait_for_rss(){
if (jQuery('#linkwidget a').length > 0 || COM_MYSITE_RSS_CHECK_COUNT > COM_MYSITE_RSS_CHECK_MAX )
{
clearInterval(COM_MYSITE_RSS_CHECK);
jQuery('#linkwidget a').click(function(){
alert('Hello World');
return false;
});
}
COM_MYSITE_RSS_CHECK_COUNT++;
}
and then after you call the ajax request,
COM_MYSITE_RSS_CHECK = setInterval(wait_for_rss, COM_MYSITE_RSS_CHECK_FREQ);
Or if individually binding to the elements is not required, you could look into the jQuery Live method of binding functions, which does not require the elements to have already been created prior to binding.
精彩评论