Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/87.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
Javascript 添加';李';最后一个LI标记之前的元素_Javascript_Jquery_Jquery Selectors - Fatal编程技术网

Javascript 添加';李';最后一个LI标记之前的元素

Javascript 添加';李';最后一个LI标记之前的元素,javascript,jquery,jquery-selectors,Javascript,Jquery,Jquery Selectors,单击“添加注释”按钮后,我希望在最后一个之前添加 参见代码: <div class="newsComment"> <ul class="CommentReplies"> <li class="Heading"> <h3> Comments </h3> </li> <li class="CommentRow"> Comment 1 </li>

单击“添加注释”按钮后,我希望在最后一个
  • 之前添加
  • 参见代码:

    <div class="newsComment">
      <ul class="CommentReplies">
         <li class="Heading">
           <h3> Comments </h3>
         </li>
          <li class="CommentRow"> Comment 1 </li>
          <li class="CommentRow"> Comment 2 </li>
          // ADD LI HERE WHEN CLICKING ON THE BUTTON
         <li class="CommentRow">
          <form id="FormAddComment">
           <input name="comment" type="text" />
           <input id="submit_AddNewsComment" type="submit" value="Add Comment" />
           </form>
         </li>
       </ul>
    </div>
    
    
    $("#FormAddComment").submit(function() {
        data =  $(this).serialize();
        //Add LI
        return false;
    });
    
    
    
    • 评论
    • 评论1 评论2 //单击按钮时在此处添加LI
    $(“#FormAddComment”).submit(函数(){ 数据=$(this).serialize(); //加李 返回false; });
    由于表单位于
    li
    中,请使用查找表单,然后在表单前面插入

    $("#FormAddComment").submit(function() {
        var $this = $(this);
    
        data =  $this.serialize();
    
        //Add LI
        $this.closest("li").before(
            "<li>New content</li"
        );
    
        return false;
    });
    
    $(“#FormAddComment”).submit(函数(){
    var$this=$(this);
    data=$this.serialize();
    //加李
    $this.最近的(“li”)。之前(
    
  • 新内容由于表单位于
    li
    中,请使用查找表单,然后在其前面插入

    $("#FormAddComment").submit(function() {
        var $this = $(this);
    
        data =  $this.serialize();
    
        //Add LI
        $this.closest("li").before(
            "<li>New content</li"
        );
    
        return false;
    });
    
    $(“#FormAddComment”).submit(函数(){
    var$this=$(this);
    data=$this.serialize();
    //加李
    $this.最近的(“li”)。之前(
    “
  • 新内容
    $(this).最近('.CommentRow')。在($('
  • '))之前;
  • 应该这样做。

    $(this).最近('.CommentRow')。在($('
  • '))之前;
  • 应该这样做。

    $(“\FormAddComment”)
    提交函数中,这将插入
  • $(this).closest('.CommentReplies').find('li:last').prepend('<li>Comment 3</li>');
    
    $(this).closest('.commentrepress').find('li:last').prepend('
  • comment3
  • ');
    $(“#FormAddComment”)
    提交函数中,这将插入
  • $(this).closest('.CommentReplies').find('li:last').prepend('<li>Comment 3</li>');
    
    $(this).closest('.commentrepress').find('li:last').prepend('
  • comment3
  • ');
    insertBefore
    不会是另一种情况吗?例如,
    $(“
  • ”).insertBefore($(this));
  • ?@David:谢谢,是的,修复了。很容易有一半的时间,我会弄错,不仅仅是因为API的措词有误。在英语中,你说“在那之前插入这个”很明显,这个在左边,那个在右边。:-)谢谢T.J.克劳德。$这个应该是$(这个)。同样抱歉,现在我明白你为什么得到$这个了-我以为它是打字错误!难道不是另一种方式吗?例如,
    $('
  • ')。insertBefore($(这个))
  • ?@David:谢谢,是的,修正了。很容易,有一半的时间,我会弄错,不仅仅是因为API的措词是简单的错误。在英语中,你说“在那之前插入这个”,所以很明显这个在左边,那个在右边。:-)谢谢T.J.克劳德。$这个应该是$(这个).也很抱歉,现在我明白你为什么会得到$this了-我还以为是打字错误!