我有一个 jQuery 代码:
$('#users').append(users.join('')).filter(function(i) {
return ids.indexOf(this.id) === -1;
}).remove();
如何使用 Mootools javascript 库重写此代码? 谢谢。
完整代码如下:
...
success: function(r) {
var users = [],
ids = [];
for(var i=0; i< r.users.length;i++){
if(r.users[i]){
users.push(_chat.render('user', r.users[i]));
ids.push('user-' + r.users[i].name);
}
}
$('#users').removeClass('sending2').append(users.join('')).children().filter(function(i) {
return ids.indexOf(this.id) === -1;
}).remove();
}...
最佳答案
不确定用户将包含什么 - 假设元素的 ID 类似于 user-name
var users = ["<div id='user-john'>john</div>", "<div id='user-bob'>bob</div>"],
ids = ['user-john'];
$("users").adopt(new Element('div', {html: users.join('')}).getChildren().filter(function(user){
return ids.indexOf(user.get('id')) !== -1; // unless it's another this.id...
}));
不确定是否相同,但应该可以帮助您入门。它只注入(inject)过滤后的。如果您想要相反的结果,请将过滤器反转为 === -1; - 它使用虚拟 div 元素来托管数组中的动态元素,并在 dom 外部而不是内部过滤它们,这样会更慢。
关于javascript - 将 jQuery 代码转换为 Mootools,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17803302/