使用Jquery将注释回复表单插入页面

时间:2009-01-19 14:39:31

标签: jquery multithreading forms comments

我一直在制作一个线程评论系统,作为正确学习php和javascript / jquery的一种方式。我过去曾做过一些事情,但是我已经做了一个新的决定来正确地学习它。

我无法将回复表单插入正在回复的评论下方的评论树中。我知道这可能是非常基本的,但是当有人点击链接时如何将html插入页面。

此代码对我不起作用:

$(document).ready(function(){
    $(function() {
        $('a#reply').click(function() {

            $(this).append("the html blah");    

        });
    });

}); 

任何人都会看到我出错的地方?

3 个答案:

答案 0 :(得分:3)

$(document).ready(function(){
        $('a#reply').click(function() {
            $(this).after("the html blah");
            return false;
        });
});

您可能需要'return false;'在那里,当你点击链接时停止页面重新加载。

答案 1 :(得分:2)

如果其他任何建议都不起作用,则链接的选择器可能无法正常工作。在您提供的示例中,您似乎正在寻找这样的链接:

<a id='reply' href='#'>add comment</a>

这是对的吗?你没有提到你是如何生成html的,但是我知道asp.net的服务器端id!=客户端id,你需要像这样修改你的选择器:

$('a[id$=reply]')

无论如何,这里有一个完整的例子,html对我来说很好用:

<html>
    <head>
        <script type="text/javascript" src='scripts\jquery-1.2.6.min.js'></script>
    </head>
    <body>
        <div style='height:400px'>
            Space to demonstrate that clicking the link does not scroll the page.
        </div>
        <div id='main'>
            <a href='#' id='clicky'>here</a>
        </div>
    </body>
    <script>
        $(function(){
            $('a#clicky').click(function(){
                $(this).after('Hello There')
                return false;
            });
        });
    </script>
</html>

答案 2 :(得分:1)

要添加到rodbv的答案,您将添加文本“html blah blah”中点击的链接,因为您使用的是append。将其更改为:

$(this).after("the html");

它会在链接之后写出html