将.data放入此对象

Putting .data to .this objects

本文关键字:对象 data      更新时间:2023-09-26

我在facebook上有一个类似wall的东西。每个帖子下面都有一些评论。我想做的是编辑评论选项。它用textarea表单替换当前的评论,并在accept之后显示新的评论。这个按钮不太好用。它应该是动态的(工作不刷新网站)。然而,我遇到了一些困难。编辑链接在一些其他评论下打开文本区域,或者在文本区域框中显示第一条评论(新评论应该出现…)我现在要做的是把。data()钉到"this"按钮,它将包含新的评论文本,所以我将能够动态地读取它到打开的文本区。我不完全确定我在做什么,我不确定使用这个,但我希望你能帮助我,伙计们!警报显示"未知数据"。也许有更好的解决方案,不需要在js大师水平的技能?

<a href="#!" class="editButton" onClick="editComment({{$comment->h_id}}, `{{$comment->f_text}}`)">
<script> 
function editComment(id, text){
    var thisId = "#" + id;
    $(thisId).replaceWith("<form class='newCommentForm'>'n'
                          <textarea id='newComment'>" + text + "</textarea>'n'
                          <input type='button' id='submit' value='save'>'n'
                          </form>");
          $('#submit').click(function() {
      var newComment =  document.getElementById('newComment').value;               
              $.ajax({
                  type: 'get',
                  url: '/editcomment',
                  data: {newComment: newComment,
                                 id: id},
                  success:function(){
                           $(".newCommentForm").replaceWith("<span id='" + id + "' class='limitedText shorter' style>" + newComment + "</span>");
                           $(".editButton", this).data("PinComment", "newComment");
                           alert($(".editButton", this).data("PinComment"));
                         },
                  error:function(){
                           alert('error');
                         }
              });                     
          });

}
</script>

在ajax调用中,this不再引用DOM元素。

编辑:

首先将注释对象放入变量

$thisComment = $(thisId);

然后改变:

$(".editButton", this)

为:

$(".editButton", $thisComment)