2017-06-14 2 views
1

ユーザーがコメントを作成するたびに、prependのjQueryを使用してコメントが追加されます。jQueryでcommentCrossを削除するには?

特定のケースでcrossIconを削除しようとしていますが、削除されません。

that.find(".commentCross").css("display","none"); 

これを行う正しい方法ではないようです。

commentCrossを削除するにはどうすればよいですか?$(this)

if ("<%= user %>" != "none") { 
    var i = 0; 
    var postKey = "<%= post.key %>"; 
    var commentsRef = firebase.database().ref("comments/"+postKey).orderByChild('commenttimestamp').limitToFirst(25); 
    commentsRef.once("value", function(snapshot) { 
    var currentUserUid = firebase.auth().currentUser.uid; 
    snapshot.forEach(function(childSnapshot){ 
    i++; 
    if (childSnapshot.val().profilepic == undefined || childSnapshot.val().profilepic == null || childSnapshot.val().profilepic == "") { 
     $("#commentsBox").prepend("<div class='fullComment' id='"+i+"'><a href='../../users/'"+childSnapshot.val().author+"><div class='userCommentBox'><div class='commentUsername'>"+childSnapshot.val().username+"</div><img class='userPic' src='../../../public/assets/miniProfilePic.png' /></div></a><div class='comment'>"+childSnapshot.val().text+"</div><div><img data-author='"+ childSnapshot.val().author +"' data-post='"+postKey+"' data-comment='"+childSnapshot.key+"'class='commentCross' src='./../../public/assets/cross.png'><img class='replyIcon' data-author='"+ childSnapshot.val().author +"' data-comment='"+childSnapshot.key+"' src='./../../public/assets/replyIcon.png'></div>"); 
     var that = $(this); 
     var commentAuthor = childSnapshot.val().author; 
     if (currentUserUid !== commentAuthor) { 
      that.find(".commentCross").css("display","none"); 
     } 
+1

をここに '$(この)'の範囲でしょうか?関連するコードをもう少し追加できますか? –

+0

なぜprepend()からcrossIconのhtmlを直接削除しないのですか?そして、特定のケースでcrossIconを削除しようとしています.->どのようなケースですか? –

+0

@AlivetoDieフェア十分な、文脈を追加しましょう – TheProgrammer

答えて

1

以下のようにそれを実行します -

if (childSnapshot.val().profilepic == undefined || childSnapshot.val().profilepic == null || childSnapshot.val().profilepic == "") { 
    var commentAuthor = childSnapshot.val().author; 
    if (currentUserUid !== commentAuthor) { 
     /*remove commentCross html */ 

     $("#commentsBox").prepend("<div class='fullComment' id='"+i+"'><a href='../../users/'"+childSnapshot.val().author+"><div class='userCommentBox'><div class='commentUsername'>"+childSnapshot.val().username+"</div><img class='userPic' src='../../../public/assets/miniProfilePic.png' /></div></a><div class='comment'>"+childSnapshot.val().text+"</div><div><img class='replyIcon' data-author='"+ childSnapshot.val().author +"' data-comment='"+childSnapshot.key+"' src='./../../public/assets/replyIcon.png'></div>"); 

    }else{ 
     /* Add commentCross Html */ 

     $("#commentsBox").prepend("<div class='fullComment' id='"+i+"'><a href='../../users/'"+childSnapshot.val().author+"><div class='userCommentBox'><div class='commentUsername'>"+childSnapshot.val().username+"</div><img class='userPic' src='../../../public/assets/miniProfilePic.png' /></div></a><div class='comment'>"+childSnapshot.val().text+"</div><div><img data-author='"+ childSnapshot.val().author +"' data-post='"+postKey+"' data-comment='"+childSnapshot.key+"'class='commentCross' src='./../../public/assets/cross.png'><img class='replyIcon' data-author='"+ childSnapshot.val().author +"' data-comment='"+childSnapshot.key+"' src='./../../public/assets/replyIcon.png'></div>"); 
    } 
} 
+0

それはうまくいくでしょうが、私はDRYerソリューションを望んでいました:) – TheProgrammer

+0

Nevermind、私はこの特定の目的のIDを既に設定していたことを忘れていました。あなたのソリューションはとにかく動作するので、私はそれを受け入れ、upvoteします。良い一日を^^ – TheProgrammer

+0

@TheProgrammerはあなたを助けてうれしい:) :) –

関連する問題