I'm working at a comment system for a website. I am using jquery to update the view after comment under post is posted.

我正在为一个网站的评论系统工作。我发布帖子后发表评论后,我正在使用jquery来更新视图。

The element I am pushing the comment after post is like this:

我在帖子后发表评论的元素是这样的:

<p class="card-text new_comment_{{$i->id}}" style="display:none"></p>

So what I do is

所以我做的是

//Manages post in view
jQuery(function ($) {
    $(document).ready(function () {

        $("body").on("submit", ".dynamic-form", function (e) {

            var form_id = '#' + $(this).attr('id');
            //removes the 'post_' part from id
            var id = this.id.replace('post_', '');
            var new_comment_class = '.new_comment_' + id;
            var new_comment_class_removal = 'new_comment_' + id;
            var comments_id = '#comments_' + id;
            var form = $(this);

            $.ajax({
                url: form.prop('action'),
                type: 'post',
                dataType: 'json',
                data: $(this).serialize(),
                success: function (data) {

                    var resultStr = "";

                    resultStr = resultStr + "<a href=\"#\">" + data.user_name + " <\/a>" + data.body ;


                    $(comments_id).find(new_comment_class).html(resultStr).slideToggle(150).promise().done(function () {
                        $(new_comment_class).fadeIn("fast").toggleClass(new_comment_class_removal);
                    });

                    $(form_id).slideToggle(150).promise().done(function () {
                        $(form_id).fadeOut("fast");
                    });

                }
            });
            e.preventDefault();
        });
    });
});

so after the javascript gets executed the element loses the class that permits me to find it in the view so that if the user posts again the body of the post doesn't get appended to that element. But now how do I put a new target element as the original one just above the newly creatded one? like this:

所以在执行javascript之后,元素会丢失允许我在视图中找到它的类,这样如果用户再次发布帖子的主体就不会附加到该元素。但是现在我如何将一个新的目标元素作为原始元素放在新生成的元素之上?像这样:

<p class="card-text new_comment_{{$i->id}}" style="display:none"></p>
<p class="card-text">This is the body of the comment</p>

1 个解决方案

#1


0

solved using http://api.jquery.com/before/ and some temp variables

使用http://api.jquery.com/before/和一些临时变量解决

更多相关文章

  1. 在关联数组中移动元素[重复]
  2. 在AngularJS中轻松控制dom - 单击按钮,然后将焦点设置为输入元素
  3. 如何设置动画以使元素围绕圆圈移动?
  4. jQuery选择器,用于查找与选择器匹配的给定元素之后的第一个元素
  5. 动态定位Bootstrap工具提示(用于动态生成的元素)
  6. Javascript XML DOM将属性设置为特定元素
  7. 获取JavaScript数组元素的最大长度
  8. 获取html DOM元素内容的边框
  9. 是否有办法阻止contenteditable的插入符号出现在IE10的元素上?

随机推荐

  1. android studio 报编码 GBK 的不可映射字
  2. Android APK安装失败,共享用户不兼容 INST
  3. android 状态栏透明,源码修改教程
  4. Android控件基本属性介绍
  5. Android:Debug certificate expired on D
  6. android 获取设备型号
  7. android > 调用拨打电话 并子线程监控然
  8. 基于Phone模块的Service实现
  9. Android API 指南
  10. Android使用系统Intent实现分享功能及将