jQuery 选择和过滤问题
您好,
我正在尝试使用 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;
});
谢谢。
Greetings,
I am trying to append a new comment to a Comments UL, in a string of parent posts, using jQuery. I cannot seem to figure out how to append to the parent post's comments UL, here is the 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>
I am trying to append the value of commentBody
(form textarea) to commentsBlock
(UL). I was able to retrieve the value of commentBody
with the following script, issue is appending it to the previous 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;
});
Thanks..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您所需要做的就是在当前警报所在的位置添加此内容:
jQuery 的
.text()
将自动转义注释中的任何 HTML。All you need is to add this where the alert currently is:
jQuery's
.text()
will automatically escape any HTML in the comment.closest()
(docs) 方法来获取最近的statusUpdate-middle
祖先find()
(docs) 方法查找嵌套的commentsBlock
append()
(docs) 追加方法评论。closest()
(docs) method to get the neareststatusUpdate-middle
ancestorfind()
(docs) method to find the nestedcommentsBlock
append()
(docs) method to append the comment.我认为你应该这样做:
注意使用类来解决性能问题,最好使用 id 来标识唯一的块,因为包含注释,所以你可以执行 .appendTo('#commentsBlock') 。
I think you should do something like this:
Pay attention in using class for performance issues, better to use id to identify a unique block, as the containing the comments, so you could do .appendTo('#commentsBlock').
尝试:
try: