2017-06-14 17 views
1

每次用戶創建評論時,都會使用帶有prepend的jQuery添加它。如何使用jQuery刪除commentCross?

我想在某些情況下刪除crossIcon,但它不會被刪除。

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

似乎不是正確的做法。

如何才能從$(this)中刪除commentCross

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

沒關係,我忘記了我已經爲此特定目的設置了一個ID。無論如何,您的解決方案都能正常工作,所以我會接受它,並加以滿足。祝你有個愉快的一天^^ – TheProgrammer

+0

@TheProgrammer很樂意幫助你:) :) –