jQuery的显示/隐藏评论回复 [英] jquery show/hide comment reply

查看:148
本文介绍了jQuery的显示/隐藏评论回复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我想尝试提供用于显示隐藏的注释ID特定文本区域的代码.

Hello Im trying to come up with code for showing hiding comment-id specific textareas.

我已经成功地能够在页面加载时为每个注释分配一个文本区域,然后在单击时隐藏并显示.但是我不知道如何再次隐藏它们.到目前为止,这是我想出的:

I've successfully been able to assign a text area to each comment on page load and then hide and show at click. But I dont know how to hide them again. This is what I've come up with so far:

$('#show-reply-comment').each(function(){
    $(this).click(function(e){
        e.preventDefault();
        var commentid = $(this).data('commentid');
        $('#'+commentid+'').show();
        $(this).unbind('click');
        $(this).attr('id', 'hide-reply-comment');
    });
});

$('#hide-reply-comment').each(function(){
    $(this).click(function(e){
        e.preventDefault();
        var commentid = $(this).data('commentid');
        $('#'+commentid+'').hide();
        $(this).unbind('click');
        $(this).attr('id', 'show-reply-comment');
    });
});

用户应该能够同时打开多个评论文本区域.如果有人可以给我提示如何继续操作,将不胜感激.

The user should be able to have several comment-reply textareas open at the same time. Would appreciate if someone could give me a hint on how to continue.

我忘了返回false;但我不想弄乱代码

I forgot return false; but I dont want to mess up the code

推荐答案

您无需尝试更改元素的ID并重复绑定和取消绑定事件处理程序,只需将整个代码替换为

Instead of trying to change the id of your element and repetively bind and unbind event handlers, you could simply replace your whole code by

$('.show-hide-reply-comment').click(function(){
   var commentid = $(this).data('commentid');
   $('#'+commentid).toggle();
});

请注意,我通过使用"show-hide-reply-comment"类代替了对按钮的ID的使用,因为只能将ID赋给一个元素.

Note that I replaced the use of an Id for your button by the use of a class "show-hide-reply-comment" because an Id can be given to only one element.

这篇关于jQuery的显示/隐藏评论回复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆