Twitter

jQuery 隐藏老用户评论资料框

in Javascript // 20 Comments

之前一直用的是 xiaorsz 的方法来隐藏老用户评论资料框,包括我发布的 DarkLightG-White 主题也是用这方法。该方法在 jQuery 1.2 和 1.3 下能正常工作,但在 1.4 下却有问题,我也说不清,如果你在用的是 jQuery 1.4+ 的话,把 if($('input#author[value]').length>0) 换成 if ($("#author").val()) (判断用户名输入框的值是否有值)即可。现在我把他的代码改进一下,并增加淡入淡出效果:

$(document).ready(function() {

    //判断用户名输入框是否有值
    if ($("#author").val()) {

        //隐藏资料框
        $("#author_info").hide();

        //建立一个编辑锚点,并赋值给edit
        var edit = "<span id='eidt_info' style='cursor: pointer;color: #BF514C;'>Edit your profile.</span>";

        //在welcome back后面插入“编辑锚点”edit
        $(".form_row").append(edit);
        $("#eidt_info").click(function() {

            //第一次点击 edit 显示用户资料框,第二次点击则隐藏,依次循环
            $("#author_info").animate({opacity: "toggle", height: "toggle"}, "slow");

            //点击edit,用户资料框显示后,Edit your profile.变为Close.
            if ($(this).text() == "Edit your profile.") {
                $("#eidt_info").text("Close.");
            } else {

                //点击edit, 用户资料框隐藏后,Close.变为 Edit your profile.
                $("#eidt_info").text("Edit your profile.");
            }
        });
    };
});

现在撇开以上方法。如果你的主题的 comments.php 里面用户资料框类似这样:

<input type="text" name="author" id="author" value="<?php echo $comment_author; ?>" size="22" tabindex="1" />
<label for="author">Name:<?php if ($req) echo " (required)"; ?></label>
<input class="text" type="text" name="email" id="email" value="<?php echo $comment_author_email; ?>" size="22" tabindex="2" />
<label for="email">Email:<?php if ($req) echo " (required)"; ?></label>
<input class="text" type="text" name="url" id="url" value="<?php echo $comment_author_url; ?>" size="22" tabindex="3" />
<label for="url">Website:</label>

如果这个资料框没有被某个 <div> 标签包着的话,那么我们手动加一个 <div id="author_info"></div>,如果已有 <div> 标签包着,给他加个 id="author_info"。这里不手动增加 Welcome Back 部分,在浏览器不支持 javascript 的情况下,似乎意义不大,直接用 jQuery 增加这一部分就可以了(个人观点):

 $(document).ready(function() {

    //判断用户名输入框的值是否有值
    if ($("#author").val()) {

        //把用户资料框赋值给info
        var info = $("#author_info");

        //把用户名赋值给author
        var author = $("#author").val();

        //隐藏用户资料框
        info.hide();

        //用户名样式为粗体,可要可不要
        $("#author").css("fontWeight", "bold");

        //在用户资料框<div id="author_info">下的最开始插入"Welcome back. Edit your info."
        info.before("<div id='welcome_back'>Welcome Back "+author+". <span id='edit_info' style='cursor: pointer; color: #BF514C;'>Edit your info.</span></div>");

        //把"edit your info."这个“编辑锚点”赋值给editInfo
        var editInfo = $("#edit_info");

        //点击“编辑锚点”editInfo
        editInfo.click(function() {

            //资料框弹出效果和关闭效果
            info.animate({opacity: "toggle", height: "toggle"}, "slow");

            //资料框弹出时锚点显示为Close.
            if (editInfo.text() == "Edit your info.") {
                $(this).text("Close.");
            } else {

                //隐藏后锚点显示为Edit your info.
                $(this).text("Edit your info.");
            }
        });
    }
});

其实就是比第一个方法在 comments.php 里面少见一个 Welcome back 的 php 语句。新手,欢迎探讨指教。
PS. 实现以上方法的前提是你在 header 或者 footer 加载了 jQuery 库。这里推荐用 jQuery 1.3.2,直接加载 google 的:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>

20 responses to jQuery 隐藏老用户评论资料框

  1. Bryan / August 22, 2010 / Reply

    发现你的主题的sidebar上面的随机文章的英文都是错误的….不是Radom posts,是Random posts
    代码里面也是错的 哈哈

  2. 星野苍真 / September 21, 2010 / Reply

    成功了,之前貌似是我本地调试用的jQuery有问题,弄来弄去把几段代码搞混了…………换google之后重头弄了一次成功了

  3. 酷剑 / March 17, 2011 / Reply

    请问你的评论框隐藏方法是什么?能否分享一下你的方法,我的有点不行哦。

Trackbacks/Pingbacks:

  1. zSnos(Veezy Edition) 主题折腾手记 | Weitian Zhao's Blog

Leave a Reply