jQuery 选择和过滤问题

标签 jquery jquery-selectors

问候,

我正在尝试使用 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/

相关文章:

jquery - Microsoft JScript 运行时错误 : Unable to set value of the property 'control' : object is null or undefined

javascript - Jquery next() 问题

javascript - JavaScript(或 jQuery)中是否有 'has focus'?

jquery - 使用 jQuery 获取选定的选项 ID

javascript - 将第一个文本节点包裹在与另一个 div 混合内容的元素内

jQuery获取li元素Ajax平滑更新

javascript - 溢出滚动框左右移动 div

javascript - 如何使用 javascript 检查 URL 是否可用

javascript - 如何使用 Jquery 选择内联文本?

javascript - jQuery - 如何通过文本选择下拉列表项?