单击()获取类名

发布于 2025-01-04 19:13:52 字数 2491 浏览 0 评论 0原文

更新:评论者告诉我更改一些代码,这是新代码,但它也不起作用。

我正在创建一个类似 Facebook 的聊天。它从 JSON 文件获取最新消息“未读”,并将文本通过“LI”附加到框中的“UL”元素。如果该框不存在,它将创建并附加文本。我希望当我单击该 div 时,它会使用 margin-bottom 负数隐藏,当我再次单击它时,它会显示为 Margin-Bottom:0。请帮助我,因为它不起作用。

function showChat(id){
$(this).animate({marginBottom : "0"}).removeClass("hidden_box").addClass("active_box").removeAttr('onclick').click(function(){
       hideChat(Id);
    });

}
function hideChat(id){
$(this).animate({marginBottom : "-270px"}).removeClass("active_box").addClass("hidden_box").click(function(){
       showChat(Id);
    });

}

function getOnJSON(){

//Creating Variables that will be used
var from;var to;var msg_id;var msg_txt;

//Getting the data from the json file
$.getJSON("/ajax/chat.json.php",function(data){

//Repeat for each result
$.each(data.notif, function(i,data){

//Getting a var to info
from = data.from;to = data.to;msg_id = data.id;msg_txt = data.text;

//check if div exists
        if ($("#chat_"+from+"_lp").length === 0){

//If not, create the div
            $("#boxes").append('<div id="chat_'+from+'_lp" class="chat_box hidden_box clickable_box"></div>');

//Add the senders name
            $("#chat_"+from+"_lp").append('<div id="'chat_+from+'_nick" class="chat_name">'+from+'</div>');

//Add the chats UL
            $("#chat_"+from+"_lp").append('<ul id="chat_'+from+'_txt" class="chat_txt"></ul>');

//Add the message text
            $("#chat_"+from+"_lp").append('<li id="' + msg_id + '">'+ msg_txt+'</li>');

//Add event handler for each div
            $('#chat_'+from+'_lp').click(function() {showChat(this);});

//If div exists just add the text
        }else{

//Add the message text
            $("#chat_"+from+"_txt").append('<li id="' + msg_id + '">'+ msg_txt+'</li>');

//Add event handler for each document
            $('#chat_'+from+'_lp').click(function() {showChat(this);});

//Close If
        }
//Close data for each item      
    });

//Close JSON
});

//Close Function
}

更新2:为了停止创建和追加内容,我制作了一个将要追加的唯一 HTML 字符串。

new_chat_string = '<div id="chat_'+from+'_lp" class="chat_box hidden_box clickable_box"><div id="'chat_+from+'_nick" class="chat_name">'+from+'</div><ul id="chat_'+from+'_txt" class="chat_txt"><li id="' + msg_id + '">'+ msg_txt+'</li></ul></div>';

$("#boxes").append(new_chat_string);    

UPDATE: A commenter told me to change some codes, this is the new code and its not working neither.

I'm creating a Facebook-Like chat. It gets the latest messages "Not Read" from a JSON file and it appends the text to an "UL" element vía "LI" into a box. If the box doesn't exist, it creates and attach the text. I want that when I click that div, it hides using margin-bottom negative, and when I click it again it shows by Margin-Bottom:0. Please help me since it's just not working.

function showChat(id){
$(this).animate({marginBottom : "0"}).removeClass("hidden_box").addClass("active_box").removeAttr('onclick').click(function(){
       hideChat(Id);
    });

}
function hideChat(id){
$(this).animate({marginBottom : "-270px"}).removeClass("active_box").addClass("hidden_box").click(function(){
       showChat(Id);
    });

}

function getOnJSON(){

//Creating Variables that will be used
var from;var to;var msg_id;var msg_txt;

//Getting the data from the json file
$.getJSON("/ajax/chat.json.php",function(data){

//Repeat for each result
$.each(data.notif, function(i,data){

//Getting a var to info
from = data.from;to = data.to;msg_id = data.id;msg_txt = data.text;

//check if div exists
        if ($("#chat_"+from+"_lp").length === 0){

//If not, create the div
            $("#boxes").append('<div id="chat_'+from+'_lp" class="chat_box hidden_box clickable_box"></div>');

//Add the senders name
            $("#chat_"+from+"_lp").append('<div id="'chat_+from+'_nick" class="chat_name">'+from+'</div>');

//Add the chats UL
            $("#chat_"+from+"_lp").append('<ul id="chat_'+from+'_txt" class="chat_txt"></ul>');

//Add the message text
            $("#chat_"+from+"_lp").append('<li id="' + msg_id + '">'+ msg_txt+'</li>');

//Add event handler for each div
            $('#chat_'+from+'_lp').click(function() {showChat(this);});

//If div exists just add the text
        }else{

//Add the message text
            $("#chat_"+from+"_txt").append('<li id="' + msg_id + '">'+ msg_txt+'</li>');

//Add event handler for each document
            $('#chat_'+from+'_lp').click(function() {showChat(this);});

//Close If
        }
//Close data for each item      
    });

//Close JSON
});

//Close Function
}

UPDATE 2: in order to stop making and appending things, I made an unique HTML string that is going to be appended.

new_chat_string = '<div id="chat_'+from+'_lp" class="chat_box hidden_box clickable_box"><div id="'chat_+from+'_nick" class="chat_name">'+from+'</div><ul id="chat_'+from+'_txt" class="chat_txt"><li id="' + msg_id + '">'+ msg_txt+'</li></ul></div>';

$("#boxes").append(new_chat_string);    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦忆晨望 2025-01-11 19:13:52

使用 class 而不是 id

<div id="chat_sender_lp" class="chat_box hidden_box clickable_box sender-click"

然后

$('.hidden_box.sender-click').live('click', function(){
   $(this).slideToggle(500);
});

use class instead of id

<div id="chat_sender_lp" class="chat_box hidden_box clickable_box sender-click"

then

$('.hidden_box.sender-click').live('click', function(){
   $(this).slideToggle(500);
});
对你的占有欲 2025-01-11 19:13:52

之后:

$("#boxes").append('<div id="chat_'+from+'_lp" class="chat_box hidden_box clickable_box" ><div id="name">'+from+'</div><ul id="chat_'+from+'_txt" class="chat_txt"><li id="' + msg_id + '">'+ msg_txt+'</li></ul></div>');

为插入的元素添加事件处理程序:

$('#chat_'+from+'_lp').click(function() { showChat(this)  })

“this”将 DOM 引用传递给其自身。

请记住,您每次都会添加:

。 ID 必须是唯一的。请改用类名。

编辑:

附加到 DOM 确实非常慢。实际上,将 HTML 构建为字符串并一次性插入会更有效。此外,您只需要在包装元素上粘贴 ID 即可。其他一切都可以使用 jQuery 选择器从中派生。它可以帮助您编写更清晰的代码。

这是您需要附加的字符串:

'<div id="chat_'+msg_id+'" class="chat_box hidden_box clickable_box">
<div class="chat_name">'+from+'</div><ul class="chat_txt"><li>
<a href="javascript://">'+msg_txt+'</a></li></ul></div>'

如果您想稍后选择聊天名称,您可以使用: $('chat_1 .chat_name').html()

连接起来也更具语义意义您的点击处理程序到 A 标记。所以你会使用:

$('#chat_'+msg_id).find('a').click(function() {showChat(this);});

这种方式代码更加清晰并且更容易遵循。我希望这有帮助。

After:

$("#boxes").append('<div id="chat_'+from+'_lp" class="chat_box hidden_box clickable_box" ><div id="name">'+from+'</div><ul id="chat_'+from+'_txt" class="chat_txt"><li id="' + msg_id + '">'+ msg_txt+'</li></ul></div>');

Add the event handler for the inserted element:

$('#chat_'+from+'_lp').click(function() { showChat(this)  })

"this" passes a DOM reference to itself.

Keep in mind that you're adding: <div id="name"> every time. IDs must be unique. Use a class name instead.

EDIT:

Appending to the DOM is really quite slow. It's actually more efficient to build up your HTML as a string and just insert it in one go. Also, you only really need to stick and ID on the wrapping element. Everything else can be derived from that using a jQuery selector. It helps you write much cleaner code.

Here's the string you need to append:

'<div id="chat_'+msg_id+'" class="chat_box hidden_box clickable_box">
<div class="chat_name">'+from+'</div><ul class="chat_txt"><li>
<a href="javascript://">'+msg_txt+'</a></li></ul></div>'

If you wanted to select chat name later, you'd use: $('chat_1 .chat_name').html()

It also makes more semantic sense to hook up your click handler to an A tag. So you'd use:

$('#chat_'+msg_id).find('a').click(function() {showChat(this);});

The code is a lot cleaner and easier to follow this way. I hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文