我有一个类似Facebook的聊天。 (你可以看到它@ http://www.live-pin.com/ )。它得到的最后消息从JSON文件并插入到个体的UL为每个用户,它检查之前,如果微升存在并且如果它不,它创建。现在的问题是,当我点击1聊天吧,3个开放的同时,也只有接近,如果我点击最后一个,我该怎么办?我想这在聊天栏点击,但如果点击犯规上not_clickable当这条唯一的开/关。感谢您的帮助
$(文件)。就绪(函数(){
getOnJSON();
的setInterval(getOnJSON(),60000);
变种标识;
VAR目标;
});
功能showChat(OBJ){
$(OBJ).animate({marginBottom:0})removeClass移除(hidden_box)addClass(active_box)解除(点击)/ *点击(函数(){。
hideChat(本);
})* /;
}
功能hideChat(OBJ){
$(OBJ).animate({marginBottom:-270px})removeClass移除(active_box)addClass(hidden_box)解除(点击)/ *点击(函数(){。
showChat(本);
})* /;
}
传播getOnJSON(){
无功自我=这一点; //添加了这一行,因为这样的变化范围在每()
VAR的; VAR来; VAR MSG_ID; VAR msg_txt; VAR new_chat_string;
//从JSON文件中获取数据
$ .getJSON(/ AJAX / chat.json.php,功能(数据){
$每个(data.notif,功能(我的数据){
从= data.from;要= data.to; MSG_ID = data.id; msg_txt = data.text;
如果($(#聊天_+在+_ LP)。长度=== 0){
$(#箱)追加('< DIV ID =聊天_'+在+'_专辑级=chat_box hidden_box clickable_box>< DIV ID =聊天_'+在+'_缺口级=chat_name >'+从+'< / DIV>< DIV CLASS =not_clickable>< UL ID =聊天_'+在+'_ TXT级=chat_txt><李ID =+ MSG_ID +'>+ msg_txt +'< /李>< / UL><窗体类=chat_new_messageNAME =new_msg><输入类型=文本占位符=请输入您的信息。 ..级=chat_new_input/>< /形式GT;< / DIV>< / DIV>');
$(+在+'_缺口'hidden_box #chat _。)生活(点击,函数(){showChat('#聊天_'+在+'_ LP');});
$(+在+'_缺口'active_box #chat _。)生活(点击,函数(){hideChat('#聊天_'+在+'_ LP');});
}其他{
$(#聊天_+在+_ TXT)追加。('<李ID =+ MSG_ID +'>+ msg_txt +'< /李>');
$(+在+'_缺口'hidden_box #chat _。)生活(点击,函数(){showChat('#聊天_'+在+'_ LP');});
$(+在+'_缺口'active_box #chat _。)生活(点击,函数(){hideChat('#聊天_'+在+'_ LP');});
}
});
});
}
解决方案
您需要使用jQuery .live()函数来适应你。点击()被应用于文档加载后创建的元素完整的。
例如: $(a.offsite)生活(点击,函数(){警报(!再见);});
是从这里 一个例子
I have a Facebook-Like Chat. (You can see it @ http://www.live-pin.com/). It gets the last messages from a JSON file and inserts into an individual UL for each user, it before checks if the ul exists and if it doesnt, it creates. Now the problem is that when I click on 1 chat bar, the 3 open at the same time, and only close if I click on the last one, what can I do? I want that this bars only open/close when clicked on chat bar but doesnt if click on not_clickable. Thanks for your help
$(document).ready(function(){
getOnJSON();
setInterval("getOnJSON()", 60000);
var Id;
var target;
});
function showChat(obj){
$(obj).animate({marginBottom : "0"}).removeClass("hidden_box").addClass("active_box").unbind('click')/*.click(function(){
hideChat(this);
})*/;
}
function hideChat(obj){
$(obj).animate({marginBottom : "-270px"}).removeClass("active_box").addClass("hidden_box").unbind('click')/*.click(function(){
showChat(this);
})*/;
}
function getOnJSON(){
var self = this; // Added this line, as this changes scope in each()
var from;var to;var msg_id;var msg_txt;var new_chat_string;
//Getting the data from the json file
$.getJSON("/ajax/chat.json.php",function(data){
$.each(data.notif, function(i,data){
from = data.from;to = data.to;msg_id = data.id;msg_txt = data.text;
if ($("#chat_"+from+"_lp").length === 0){
$("#boxes").append('<div id="chat_'+from+'_lp" class="chat_box hidden_box clickable_box"><div id="chat_'+from+'_nick" class="chat_name">'+from+'</div><div class="not_clickable"><ul id="chat_'+from+'_txt" class="chat_txt"><li id="' + msg_id + '">'+ msg_txt+'</li></ul><form class="chat_new_message" name="new_msg"><input type="text" placeholder="Enter your message..." class="chat_new_input"/></form></div></div>');
$('.hidden_box #chat_'+from+'_nick').live("click", function(){ showChat('#chat_'+from+'_lp'); });
$('.active_box #chat_'+from+'_nick').live("click", function(){ hideChat('#chat_'+from+'_lp'); });
}else{
$("#chat_"+from+"_txt").append('<li id="' + msg_id + '">'+ msg_txt+'</li>');
$('.hidden_box #chat_'+from+'_nick').live("click", function(){ showChat('#chat_'+from+'_lp'); });
$('.active_box #chat_'+from+'_nick').live("click", function(){ hideChat('#chat_'+from+'_lp'); });
}
});
});
}
解决方案
You need to use jquery .live() function for your .click() to be applied to elements created after the document load complete.
For instance: $("a.offsite").live("click", function(){ alert("Goodbye!"); });
is an example from here