2012-12-10 56 views
1

我試圖在AJAX成功時刪除div,但在這樣做時遇到了麻煩。成功時,我看到「已添加」兩個詞出現兩次,因爲我無法選擇正確包含它的2個div。有關如何正確選擇它的任何建議? 的javascript:如何選擇和刪除/隱藏與jQuery的div?

success: function(message){       
     alert("Deleting!"); 
     $this.closest('.image').find('.already_fav_content p').removeClass(); #doesn't work 

     $this.closest('.image').find('.fav_content p').hide(); #doesn't work 

     $this.closest('.image').find('.removebutton').hide(); #works 

     $this.closest('.image').find('.already_favorited').removeClass(); #works 

     $this.closest('.image').find('.fav').removeClass(); #works 


div.fav{ 
    display: none; 
    position:absolute; /* absolute position (so we can position it where we want)*/ 
    bottom:0px; /* position will be on bottom */ 
    left: 0px; 
    /* styling bellow */ 
    background-color:#E99C07; 
    font-family: "Arial Black", Gadget, sans-serif; 
    font-size:18px; 
    font-weight:900; 
    color:white; 
    opacity:0.75; /* transparency */ 
    filter:alpha(opacity=70); /* IE transparency */ 
} 
p.fav_content{ 
    padding:10px; 
    margin:0px; 
} 

div.already_favorited{ 
    position:absolute; /* absolute position (so we can position it where we want)*/ 
    bottom:0px; /* position will be on bottom */ 
    left: 0px; 
    /* styling bellow */ 
    background-color:#E99C07; 
    font-family: "Arial Black", Gadget, sans-serif; 
    font-size:18px; 
    font-weight:900; 
    color:white; 
    opacity:0.75; /* transparency */ 
    filter:alpha(opacity=70); /* IE transparency */ 
} 
p.already_fav_content{ 
    padding:10px; 
    margin:0px; 
}      

HTML:

<div class="wrapper"> 

       <!-- image --> 
       <div class="image" style="position: relative; left: 0; top: 0;"> 

        <a href="/partners/Business/forbes"> 
         <img src="http://videomuncher.com/static3/forbes.png" style="position: relative; width: 150px; top: 0; left: 0;"> 
        </a> 


        <!-- already fav div --> 
         <div class="already_favorited"> 
          <!-- fav content --> 
          <p class="already_fav_content">Added</p> <!-- end fav content --> 
         </div> 
        <!-- end already fav div --> 

        <!-- munchbutton div --> 
        <div class="munchbutton" style="display: none;"> 
         <form method="post" action="/munch_video/ " class="removebutton"><div style="display:none"><input type="hidden" name="csrfmiddlewaretoken" value="dKrS8NzqPWFLM6u8wJrAeid4nGw1avGK"></div> 
          <input type="hidden" value="Channel" class="playlist"> 
          <input type="hidden" value="forbes" class="video_id"> 
          <input type="hidden" value="forbes" class="video_title"> 
          <input type="hidden" value="remove_video" class="remove"> 

          <input type="submit" class="btn btn-danger" value="Remove"> 
         </form> 
        </div> 
        <!-- end munchbutton div --> 

        <!-- fav div --> 
        <div class="fav"> 
         <!-- fav content --> 
         <p class="fav_content">Added</p> <!-- end fav content --> 
        </div> 
        <!-- end fav div --> 

       </div> 
       <!-- end image div --> 

       </div> 

回答

1

它沒有wroked因爲選擇錯了

$this.closest('.image').find('.already_fav_content p').removeClass(); #doesn't work 

    $this.closest('.image').find('.fav_content p').hide(); #doesn't work 

將其更改爲,

$this.closest('.image').find('p.already_fav_content').removeClass(); 

    $this.closest('.image').find('p.fav_content').hide();