问候,
我正在尝试使用 jQuery 将新评论附加到父帖子字符串中的 Comments UL。我似乎不知道如何附加到父帖子的评论 UL,这里是 HTML -
<div class="statusUpdate-DIV">
<div class="statusUpdate-middle">
<span class="statusUpdate-timestamp"><?php echo date("M d, y g:i a", strtotime($row['timestamp']));?></span>
<span class="statusUpdate-title"><?php echo $row['title'];?></span>
<?php echo $row['body'];?>
<div class="commentsDIV" id="status<?php echo $row['id'];?>">
<ul class="commentsBlock">
<li><strong>This is a comment</strong> comment here</li>
</ul>
</div>
<div class="postCommentDIV">
<span class="commentHeader">Comment</span>
<form name="commentForm" class="postCommentFORM" method="post" action="">
<textarea rows="3" cols="63" class="commentBody" name="commentBody"></textarea>
<input type="submit" value="Post"/>
</form>
</div>
</div>
</div>
我正在尝试将 commentBody
(表单文本区域)的值附加到 commentsBlock
(UL)。我能够使用以下脚本检索 commentBody
的值,问题是将其附加到之前的 commentsBlock UL 中:
//This is just a test to make sure the value of .commentBody is being retrieved
$(".postCommentDIV form").submit(function() {
var comment = $(this).find(".commentBody").val();
alert(comment);
return false;
});
谢谢..
最佳答案
您所需要的只是将其添加到当前警报所在的位置:
// Create the <li> containing the comment
var commentLi = $("<li>").text(comment);
// Travel up DOM tree to the closest statusUpdate-DIV, then back down to append
$(this).closest("statusUpdate-DIV").find(".commentsBlock").append(commentLi);
jQuery 的 .text()
将自动转义注释中的任何 HTML。
关于jQuery 选择和过滤问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4822634/