【发布时间】:2012-02-02 10:40:48
【问题描述】:
如何使用 mootools 在我的 div 容器中过滤(替换 empty() 函数)?
这里的问题:如何将下面的代码替换为:
- 从容器(
<div id="fc_contacts" class="fc_contacts">) 中删除带有id="fc_ID"且 JSON 字符串中不存在的块 - 如果容器中不存在,则将新块添加到容器(
<div id="fc_contacts" class="fc_contacts">) - 如果
friend_id在容器和 JSON 中 - 什么也不做。
请检查代码。
我的代码如下:
{"users": [{"friend_id":"62","name":"admin","username":"admin"},{"friend_id":"66","name":"other","username":"other"}],"total": "1","total_online":"1"}
onSuccess: function(f){ /*Periodical function*/
for(var i=0; i< f.users.length;i++){
if(f.users[i].friend_id){
friends.push(chat.render('add_contact',f.users[i]));
}
}
/* Question here: How can I replace my code below to:
1. Remove from container(<div id="fc_contacts" class="fc_contacts">) blocks with id="fc_ID" which not exist in JSON string
2. Add new block to container(<div id="fc_contacts" class="fc_contacts">) if if doesn't exist in container
3. If friend_id is in container and in the JSON - do nothing.
*/
$('fc_contacts').empty(); /*Replace this*/
$('fc_contacts').addContacts(friends.join(''), 'bottom'); /*ELSE - ADD NEW*/ //el.empty();
}
Element.implement({
addContacts: function(html,where){
return this.grab(new Element('<div>', {
'html': html,
'class': 'fc_contacts_container'
}),where);
}
});
<div id="fc_contacts" class="fc_contacts">
<div class="fc_contacts_container">
<div class="fc_contact clear_fix" id="fc_62">
<!--OTHER HTML!->
</div>
</div>
<div class="fc_contacts_container">
<div class="fc_contact clear_fix" id="fc_66">
<!--OTHER HTML!->
</div>
</div>
</div>
谢谢!
附言
它适用于 JQUERY(但我需要它与 Mootools 兼容):
var ids = [];
for(var i=0; i<f.users.length;i++){
ids.push('fc-' + f.users[i].friend_id);
}
jQuery('#fc_contacts').append(friends.join('')).children().filter(function(i) {
return ids.indexOf(this.id) === -1;
}).remove();
【问题讨论】:
标签: html arrays json filter mootools