如何在div中实现“read more”

时间:2013-11-03 14:05:34

标签: php jquery html ajax

我正在显示数据库中检索到的数据的一部分(作者和日期)。但我希望实现一种“阅读更多”,其中作者被链接到完整的结果,即当我点击作者&日期,评论附加到它。我会感激一个例子。感谢

    <body>
    <?php
    session_start();
    $u = $_SESSION['username'];
    if(isset ($_SESSION['username']))
      {
        $database = "xxxx";
        $Username = "username";
        $Password = "password";

        $con="host=localhost port=5432 dbname=$database user=$Username password=$Password";
        $db=pg_connect($con) or die('connection failed');
        $query = 'select a.name,p.date,p.comment from author a, post p where a.name = p.author';
        $posts = pg_query($db, $query);                   
      }
    ?>

    <div class = "result">
    <h2>Show Result</h2>
    <?php
     while($row=pg_fetch_assoc($posts))
     {
       $author=$row['a.name'];
       $date=$row['p.date'];
       echo "<p><a href='#'>$author:$date</a></p>";
     }
     ?>
    </div>
    </body>

1 个答案:

答案 0 :(得分:1)

在HTML中添加评论:

   $author=$row['a.name'];
   $date=$row['p.date'];
   $comment=$row['p.comment'];
   echo "<p><a href='#'>$author:$date</a><span class=\"comment\">$comment</span></p>";

并添加此代码JS:

$(function(){
    $(".result p > span.comment").hide(0);
    $(".result p > a").click(function(e){
        var comment = $(this).parent().find("span.comment");
        if (comment.is(":visible"))
            comment.slideUp();
        else
            comment.slideDown();
        return false;
    });
});

(你添加了标签jQuery,所以我认为你使用它)