jQuery |滤芯
也许你可以帮助我:
我的功能:
getMyUsers : function(callback){ /*- get users every 5 seconds*/
...
...
success: function(r) {
var users = [];
for(var i=0; i< r.users.length;i++){
if(r.users[i]){
users.push(render('user',r.users[i]));
}
}
jQuery('#chatUsers').append(users.join(''));
}
url: 'path/handler.php?action=getusers';
}
此功能从服务器获取我的用户。它以 json 字符串形式出现,例如:
{
"users": [{
"name": "username_temp",
"last_activity": "16:29:16",
"gravatar": "thumb_1546278d532ea4c1cddc7a4b.jpg",
"status": "",
"usertype": "M",
"typing": "0"
}, {
"name": "admin",
"last_activity": "16:29:16",
"gravatar": "thumb_7d41870512afee28b70d5d91.jpg",
"status": "",
"usertype": "M",
"typing": "0"
}],
"total": "2"
}
render: function(template,params){
switch(template){
case "user";
arr = ['<div class="user" id="user-',params.name,'">',params.name,'</div>'];
break;
}
return arr.join('');
}
我的 HTML 容器:
<div id="chatUsers"></div>
我应该如何将我的函数更改为:
- 如果字符串中没有用户,则从我的 div 容器中删除(过滤)用户。
- 如果我的用户已经在容器中,则跳过。
- 如果这是新用户,只需将其附加到容器中即可。
多谢!
May be you can help me:
My functions:
getMyUsers : function(callback){ /*- get users every 5 seconds*/
...
...
success: function(r) {
var users = [];
for(var i=0; i< r.users.length;i++){
if(r.users[i]){
users.push(render('user',r.users[i]));
}
}
jQuery('#chatUsers').append(users.join(''));
}
url: 'path/handler.php?action=getusers';
}
This function get my users from the server. It comes as json string like:
{
"users": [{
"name": "username_temp",
"last_activity": "16:29:16",
"gravatar": "thumb_1546278d532ea4c1cddc7a4b.jpg",
"status": "",
"usertype": "M",
"typing": "0"
}, {
"name": "admin",
"last_activity": "16:29:16",
"gravatar": "thumb_7d41870512afee28b70d5d91.jpg",
"status": "",
"usertype": "M",
"typing": "0"
}],
"total": "2"
}
render: function(template,params){
switch(template){
case "user";
arr = ['<div class="user" id="user-',params.name,'">',params.name,'</div>'];
break;
}
return arr.join('');
}
My HTML container:
<div id="chatUsers"></div>
How should I change my functions to:
- Remove (filter) user(s) from my div container if it not comes in the string.
- If my user already in the container skip.
- If this is a new user just append it to the container.
Thanks a lot!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我了解,如果用户不在 JSON 中,您希望将其从 DIV 中删除;如果它们在 JSON 中,您想跳过添加它们;如果它们不在 DIV 中,您想要附加它们。
那么,为什么不直接替换 DIV 的内容而不是追加到它的内容呢?
As I understand, you want to remove users from the DIV if they're not in the JSON; you want to skip adding them if they are in the JSON; and you want to append them if they're not in the DIV.
Well, why not just replace the contents of the DIV instead of appending to it?