【问题标题】:jQuery change css by indexjQuery按索引更改css
【发布时间】:2017-04-08 17:42:30
【问题描述】:

我想通过 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 的索引,但似乎没有加载库的索引。目前还不行。

【问题讨论】:

标签: javascript jquery html css indexing


【解决方案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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-14
    • 1970-01-01
    • 1970-01-01
    • 2016-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-29
    相关资源
    最近更新 更多