-1

你好我正在尝试插入

<?php comments_template(); ?>

进入下面的jQuery显示功能,但它不起作用。甚至可以将php函数插入jQuery吗?*更新

<head> 
<script src="code.jquery.com/jquery-latest.js"></script>; 
</head> 
<button>Show it</button> 
<a style="display: none"><?php comments_template(); ?></a> 
<script>
$("button").click(function () { $("a").show("slow"); }); 
</script>
4

2 回答 2

1

May be if your comments_template returns its result as a string, instead of directly printing it with echo, you just need something like this:

<?php echo comments_template(); ?>
于 2013-02-14T01:02:05.717 回答
1

Firstly, make your comments_template() return a full string.

Then, try structuring your code like this. You want to .hide() the comments on .ready(), and then .show() them when you click the .comment-button button.

<head>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  <button class="comment-button">Show Comments</button>
  <p class="comments"><?php echo comments_template(); ?></p>
  <script>
    $("document").ready(function() {
      $(".comments").hide();
      $(".comment-button").click(function () {
        $(".comments").show("slow");
      });
    });
  </script>
</body>

Don't use direct elements with $() in jQuery, either; use a class or an ID.

Also, if you want a brilliant framework to use that can collapse elements with subtle animations, try Bootstrap for Twitter and then do something like this:

<div class="comment-1 collapse">
  <?php echo comments_template(); ?>
</div>
<button data-target=".comment-1" data-toggle="collapse">Show Comment</button>

No Javascript needed, since it's already in the bootstrap library.

于 2013-02-14T01:04:42.770 回答