我有一个小问题,希望有 JQuery 知识的人可以查看我的代码并发现我做错了什么。我有两个使用 ajax 更新的无序列表(动态创建)。使用下面的脚本,我只能更新一个列表,但不能同时更新。我克隆了第一个脚本并更改了第二个列表的变量,但只有一个得到更新。谢谢你的帮助。
这是html代码:
List one: <ul data-options="" class="elgg-list-mainriver elgg-sync elgg-list-river elgg-river"><li> .....</li>
</ul>
List Two: <ul data-options="" class="elgg-list-miniriver elgg-sync elgg-list-river elgg-river"><li> .....</li>
</ul>
和jQuery代码:
elgg.provide('elgg.river');
elgg.river.init = function() {
var riverList = $('ul.elgg-list-mainriver');
//var miniriverList = $('.elgg-list-miniriver');
var time = 20000;
if (!window.rivertimer) {
window.rivertimer = true;
var refresh_river = window.setTimeout(function(){
elgg.river.timedRefresh(riverList);
//elgg.river.MinitimedRefresh(miniriverList);
}, time);
}
};
elgg.river.timedRefresh = function(object) {
var first = $('li:first', object);
if (!first.length) {
first = object;
}
var time = first.data('timestamp');
elgg.getJSON('activity', {
data : {
sync : 'new',
time : time,
options : object.data('options')
},
success : function(output) {
if (output) {
$.each(output, function(key, val) {
var new_item = $(val).hide();
//new_item = $(val).show();
object.prepend(new_item.fadeIn(600));
//object.prepend(new_item.$(val).show());
});
}
window.rivertimer = false;
elgg.trigger_hook('success', 'elgg:river:ajax');
}
});
}
elgg.provide('elgg.river_miniriver');
elgg.river_miniriver.init = function() {
var miniriverList = $('ul.elgg-list-miniriver');
var time = 20000;
if (!window.rivertimer) {
window.rivertimer = true;
var refresh_river = window.setTimeout(function(){
elgg.river_miniriver.timedRefresh(miniriverList);
}, time);
}
};
elgg.river_miniriver.timedRefresh = function(object) {
var first = $('li:first', object);
if (!first.length) {
first = object;
}
var time = first.data('timestamp');
elgg.getJSON('activity', {
data : {
sync : 'new',
time : time,
options : object.data('options')
},
success : function(output) {
if (output) {
$.each(output, function(key, val) {
var new_item = $(val).hide();
//new_item = $(val).show();
object.prepend(new_item.fadeIn(600));
//object.prepend(new_item.$(val).show());
});
}
window.rivertimer = false;
elgg.trigger_hook('success', 'elgg:river:ajax');
}
});
}
elgg.register_hook_handler('init', 'system', elgg.river.init);
elgg.register_hook_handler('success', 'elgg:river:ajax', elgg.river.init, 500);
elgg.register_hook_handler('init', 'system', elgg.river_miniriver.init);
elgg.register_hook_handler('success', 'elgg:river:ajax', elgg.river_miniriver.init, 500);
如果我尝试在函数 "$('ul.elgg-list-miniriver, ul.elgg-list-mainriver')" 中合并两个列表类,则两个列表都会更新,但第一个列表项 (li) 会得到 "opacity:0 ;" 很有型????
这段代码:
elgg.river_miniriver.init = function() {
var miniriverList = $('ul.elgg-list-miniriver, ul.elgg-list-mainriver');
var time = 20000;
if (!window.rivertimer) {
window.rivertimer = true;
var refresh_river = window.setTimeout(function(){
elgg.river_miniriver.timedRefresh(miniriverList);
}, time);
}
};
elgg.river_miniriver.timedRefresh = function(object) {
var first = $('li:first', object);
if (!first.length) {
first = object;
}
var time = first.data('timestamp');
elgg.getJSON('activity', {
data : {
sync : 'new',
time : time,
options : object.data('options')
},
success : function(output) {
if (output) {
$.each(output, function(key, val) {
var new_item = $(val).hide();
//new_item = $(val).show();
object.prepend(new_item.fadeIn(1000));
//object.prepend(new_item.$(val).show());
});
}
window.rivertimer = false;
elgg.trigger_hook('success', 'elgg:river:ajax');
}
});
}