2016-11-24 4 views
0

jQueryのインデックスで要素のCSSを変更したいと思います。jQueryはCSSでインデックスを変更します

$(".switcher span").click(function() { 
 
    var galleryIndex = $(this).index(); 
 
    $('.wrapper .gallery').eq(galleryIndex).css("background", "red"); 
 
});
<div class="switcher"> 
 
    <span>Switch 0</span> 
 
    <span>Switch 1</span> 
 
</div> 
 

 
<div class="wrapper"> 
 
    <div class="gallery">GALLERY 0</div> 
 
    <div class="gallery">GALLERY 1</div> 
 
</div>

jQueryの.switcher span罰金のインデックスを取得しますが、ギャラリーPFインデックスがロードされていないようです。これまでのところうまくいきません。

+1

を実行可能なスニペットを共有しています..:

あなたがにしたい場合は、すべてあなたが''に設定することができますクリックで背景色をリセット.. – Rayon

+0

うまく動作しますhttps://jsfiddle.net/nu4ngh31/ – Satpal

答えて

1

すべてがあなたのコードでうまくいくようです。 ...

私のために正常に動作し

$(".switcher span").click(function(){ 
 
     var galleryIndex = $(this).index(); 
 
     $('.wrapper .gallery').css('background', '').eq(galleryIndex).css("background", "red"); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 

 
<div class="switcher"> 
 
    <span>Switch 0</span> 
 
    <span>Switch 1</span> 
 
</div> 
 

 
<div class="wrapper"> 
 
    <div class="gallery">GALLERY 0</div> 
 
    <div class="gallery">GALLERY 1</div> 
 
</div>

関連する問題