2016-02-09 152 views
0

我想刪除添加的DIV's(class stripe),然後僅將它應用於可見的DIVs但我卡住了。似乎我不明白腳本如何處理。任何幫助表示讚賞!點擊按鈕後刪除DIV

https://jsfiddle.net/c98rxbju/

$('.item').each(function(i,e){ 
    if (((i+1) % 2) == 0) 
     $(this).wrapAll('<div class="stripe" style="background-color: #887733;" />\n\n'); 
}); 
$(":button").click(function() { 
    var selectedcolor = this.value; 
    var list = $(".item"); 
    $(list).fadeOut("fast"); 
    $(list).each(function(index) { 
     var color = $(this).data("color"); 
     if (color == selectedcolor){ 
      $(this).fadeIn("fast"); 
     } 
    }); 
$('.stripe').contents().unwrap(); 
$('.item:visible').each(function(i,e){ 
    if (((i+1) % 2) == 0) 
     $(this).wrapAll('<div class="stripe" style="background-color: #887733;" />\n\n'); 
    }); 
}); 
+0

究竟是你想使用這個jQuery代碼實現什麼,你的問題是不清楚的嗎? – stark

回答

1

試試這個。我簡化了你的代碼。

function UpdateStrips(){ 
    $('.item:visible:odd').each(function(){ 
     $(this).wrap('<div class="stripe"/>'); 
    }); 
} 

$(":button").click(function() { 
     var selectedcolor = this.value; 
     var list = $(".item"); 
     $(list).fadeOut("fast"); 
     $(list).each(function(index) { 
      if($(this).closest('div.stripe').length) 
       $(this).unwrap(); // unwrap only the elements with parent div with class stripe 
       var color = $(this).data("color"); 
       if (color == selectedcolor) 
       $(this).fadeIn("fast",function() 
       { 
        UpdateStrips(); // invoke callback once the fadeIn completes 
       }); 
     });   
}); 

UpdateStrips(); // invoke on page load 

工作例如:https://jsfiddle.net/DinoMyte/c98rxbju/29/

0

相反的fadeIn and fadeOut,使用show and hide with time delays,那麼你會得到規定的輸出。


$('.item').each(function(i) { 
 
    if (((i + 1) % 2) == 0) 
 
    $(this).wrapAll('<div class="stripe" style="background-color: #887733;" />'); 
 
}); 
 
$(":button").click(function() { 
 
    var selectedcolor = this.value; 
 
    var list = $(".item"); 
 
    $(list).hide(); 
 
    $(list).each(function(index) { 
 
    var color = $(this).data("color"); 
 
    if (color == selectedcolor) { 
 
     $(this).show(); 
 
    } 
 
    }); 
 
    $('.stripe').contents().unwrap(); 
 
    $('.item:visible').each(function(i) { 
 
    if (((i + 1) % 2) == 0) 
 
     $(this).wrapAll('<div class="stripe" style="background-color: #887733;" />\n\n'); 
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<button value="blue" type="button">Blue</button> 
 
<button value="black" type="button">Black</button> 
 
<button value="white" type="button">White</button> 
 
<div id="wrapper"> 
 
    <div class="item" data-color="blue">Toyota</div> 
 
    <div class="item" data-color="blue">Nissan</div> 
 
    <div class="item" data-color="blue">Volvo</div> 
 
    <div class="item" data-color="blue">Ford</div> 
 
    <div class="item" data-color="black">Chevrolet</div> 
 
    <div class="item" data-color="white">BMW</div> 
 
    <div class="item" data-color="white">Mercedes</div> 
 
    <div class="item" data-color="blue">Mercedes</div> 
 
    <div class="item" data-color="black">Nissan</div> 
 
    <div class="item" data-color="white">Fiat</div> 
 
    <div class="item" data-color="white">Volvo</div> 
 
</div>

檢查這方面的工作小提琴:here