如何在延迟后向跨度添加类?

时间:2015-05-31 20:51:50

标签: jquery html

我正在尝试在延迟后向一个范围添加一个类但没有任何反应。 Jquery的:

$(".arrow-left").delay(1000).queue(function() {
       $(this).addClass("active")
});
$(".arrow-right").delay(1000).queue(function() {
        $(this).addClass("active")
});

HTML:

<nav>
     <span class="arrow-left">
         <div class="arrow-l">
              <img alt="" src="images/prev-arrow.svg">
         </div>
     </span>
     <span class="arrow-right">
         <div class="arrow-r">
            <img alt="" src="images/next-arrow.svg">
         </div>
      </span>
</nav>

由于

1 个答案:

答案 0 :(得分:1)

你的jQuery是正确的。问题可能是div不是span的有效子项,因此可能不会继承active类的属性(取决于浏览器实现)。

更改为:

<nav>
  <div class="arrow-left">
     <div class="arrow-l">
          <img alt="" src="images/prev-arrow.svg">
     </div>
  </div>
  <div class="arrow-right">
     <div class="arrow-r">
        <img alt="" src="images/next-arrow.svg">
     </div>
  </div>
</nav>

&#13;
&#13;
$(".arrow-left").delay(1000).queue(function() {
  $(this).addClass("active")
});

$(".arrow-right").delay(1000).queue(function() {
  $(this).addClass("active")
});
&#13;
.arrow-left, .arrow-right {
  display: inline-block;
}

.active {
  zoom: 1.2;
  border: 5px solid red;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<nav>
  <div class="arrow-left">
    <div class="arrow-l">
      <img alt="" src="http://placehold.it/100x100">
    </div>
  </div>
  <div class="arrow-right">
    <div class="arrow-r">
      <img alt="" src="http://placehold.it/100x100">
    </div>
  </div>
</nav>
&#13;
&#13;
&#13;