Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jqueryselection&;过滤问题_Jquery_Jquery Selectors - Fatal编程技术网

jqueryselection&;过滤问题

jqueryselection&;过滤问题,jquery,jquery-selectors,Jquery,Jquery Selectors,您好 我正在尝试使用jQuery将一个新的注释附加到注释UL,在一系列父帖子中。我似乎不知道如何附加到父帖子的评论中,这里是HTML- <div class="statusUpdate-DIV"> <div class="statusUpdate-middle"> <span class="statusUpdate-timestamp"><?php echo date("M d, y g:i a", strtoti

您好

我正在尝试使用jQuery将一个新的注释附加到注释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>
谢谢

$(this).closest('.statusUpdate-middle').find('.commentsBlock').append(comment);
  • 获取最近的
    statusUpdate middle
    祖先的方法
  • 查找嵌套的
    commentsBlock
  • 附加注释的方法

您只需在当前警报的位置添加以下内容:

// 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);
//创建包含注释的
  • var commentLi=$(“
  • ”)。文本(注释); //向上移动DOM树到最近的statusUpdate DIV,然后向下移动到append $(this).closest(“statusUpdate DIV”).find(“.commentsBlock”).append(commentLi);
  • jQuery的
    .text()
    将自动转义注释中的任何HTML。

    尝试:

    $(".postCommentDIV form").submit(function() {
    var comment = $(this).find(".commentBody").val();
    $(this).find(".commentsBlock").append("<ul><strong>" + comment + "</strong></ul>");
    return false;
    });
    
    $(“.postCommentDIV表单”).submit(函数(){
    var comment=$(this.find(“.commentBody”).val();
    $(this).find(“.commentsBlock”).append(“
      ”+comment+”
        ”); 返回false; });
    我认为你应该这样做:

        $(".postCommentDIV form").submit(function() {
                  var comment = $(this).find(".commentBody").val();
                  $('<li>').text(comment).appendTo('.commentsDIV .commentsBlock');
                  return false;
        });
    
    $(“.postCommentDIV表单”).submit(函数(){
    var comment=$(this.find(“.commentBody”).val();
    $(“
  • ”).text(comment).appendTo(“.commentsDIV.commentsBlock”); 返回false; });

  • 在使用类处理性能问题时请注意,最好使用id来标识一个唯一的块,因为它包含注释,所以您可以这样做。appendTo(“#commentsBlock”)。

    只是澄清一下,同一页面上有几个
    statusUpdate DIV
    ,谢谢!这非常有效,只需将$(This.nextest(“statusupdatediv”)更改为$(This.nextest”(.statusupdatemiddle))。再次感谢在这种情况下,
    .find()
    无法获取
    .commentsBlock
    ,因为它不在
    中。当然!最近的()!我完全忘记了这一点,当时我正试着用parents()来解决这个问题,准备把头发拔出来。非常感谢-这将是选择的答案,但它似乎没有附加一个新的李到评论栏UL。我本可以解决这个问题,但第9箱提供了一个完整的解决方案,可以说是开箱即用。;)谢谢你的提醒和参考。@NightMICU:是的,我把
  • 创作留给你了。仅供参考,要使用
    .parents()
    ,只需执行
    .parents('.statusUpdate middle:first')
    :first
    只是为了防止嵌套元素具有相同的类名。非常好的信息!我希望Box9没有让它这么简单,应该有更多的东西来理解lolAh非常好+1用于性能考虑。问题的一部分在于有多个相同的post div,每个post div都有自己的注释和注释表单,就像这里的Stack Overflow一样。因此,我无法引用
    #commentsBlock
    ,因为同一页上会有多个注释。但是,我可以为每个帖子分配一个唯一的ID,然后检索该帖子。。这有帮助吗?还是六合一,六合一?
        $(".postCommentDIV form").submit(function() {
                  var comment = $(this).find(".commentBody").val();
                  $('<li>').text(comment).appendTo('.commentsDIV .commentsBlock');
                  return false;
        });