微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为当前的 swiperjs 幻灯片添加属性

如何解决为当前的 swiperjs 幻灯片添加属性

我正在尝试研究如何将 tabindex 为 1 添加到活动的 swiperjs 幻灯片。我查看了文档并相信我在正确的轨道上。

幻灯片发生变化时,我可以让它登录到控制台

但是当我尝试添加选项卡索引属性时,我收到了一个无法解决的控制台错误 未捕获的类型错误:无法读取 null 的属性“setAttribute”

任何建议将不胜感激

谢谢

<html>

<body>



  <link rel="stylesheet" href="https://unpkg.com/swiper/swiper-bundle.css" />
  <link rel="stylesheet" href="https://unpkg.com/swiper/swiper-bundle.min.css" />

  <script src="https://unpkg.com/swiper/swiper-bundle.js"></script>
  <script src="https://unpkg.com/swiper/swiper-bundle.min.js"></script>



  <!-- Slider main container -->
  <div class="swiper-container swipercarousel">
    <!-- Additional required wrapper -->
    <div class="swiper-wrapper">
      <!-- Slides -->


      <div class="swiper-slide">

        <h3>slide1</h3>
        <p>
          Lorem ipsum dolor sit amet,consectetur adipiscing elit. Donec imperdiet lectus purus,a dignissim magna elementum vitae.
        </p>

        <a href="/link" class="button button--color-white" rel="noopener" target="" <a href="/about/" class="button button--color-white" rel="noopener" target="" tabindex="-1">Find out more</a>

      </div>

      <div class="swiper-slide">

        <h3>slide2</h3>
        <p>
          Lorem ipsum dolor sit amet,a dignissim magna elementum vitae.
        </p>

        <a href="/link" class="button button--color-white" rel="noopener" target="" tabindex="-1">Find out more</a>

      </div>



      <div class="swiper-slide">


        <h3>slide3</h3>
        <p>
          Lorem ipsum dolor sit amet,a dignissim magna elementum vitae.
        </p>

        <a href="/link" class="button button--color-white" rel="noopener" target="" tabindex="-1">Find out more</a>

      </div>

      <div class="swiper-slide">

        <h3>slide4</h3>
        <p>
          Lorem ipsum dolor sit amet,a dignissim magna elementum vitae.
        </p>

        <a href="/link" class="button button--color-white" rel="noopener" target="" tabindex="-1">Find out more</a>

      </div>

      <div class="swiper-slide">

        <h3>slide5</h3>
        <p>
          Lorem ipsum dolor sit amet,a dignissim magna elementum vitae.
        </p>

        <a href="/link" class="button button--color-white" rel="noopener" target="" tabindex="-1">Find out more</a>

      </div>

      <div class="swiper-slide">
        <h3>slide6</h3>
        <p>
          Lorem ipsum dolor sit amet,a dignissim magna elementum vitae.
        </p>

        <a href="/link" class="button button--color-white" rel="noopener" target="" tabindex="-1">Find out more</a>

      </div>




    </div>
    <!-- If we need pagination -->
    <div class="swiper-pagination"></div>

    <!-- If we need navigation buttons -->
    <div class="swiper-button-prev"></div>
    <div class="swiper-button-next"></div>

    <!-- If we need scrollbar
    <div class="swiper-scrollbar"></div>
  -->
  </div>




  <script>
    var swipercarousel = new Swiper('.swipercarousel',{
      // Optional parameters
      direction: 'horizontal',loop: true,spaceBetween: 10,centeredSlides: true,slidesPerView: 1.2,//slidesOffsetBefore:30,keyboard: {
        enabled: true,onlyInViewport: true,},// Responsive breakpoints
      breakpoints: {
        // when window width is >= 640px
        640: {
          slidesPerView: 2,}
      },// If we need pagination
      pagination: {
        el: '.swiper-pagination',clickable: true,// Navigation arrows
      navigation: {
        nextEl: '.swiper-button-next',prevEl: '.swiper-button-prev',// And if we need scrollbar
      scrollbar: {
        el: '.swiper-scrollbar',on: {
        slideChange: function() {
          // do something
          console.log('something');
          var tab = document.querySelector(".swiper-slide-active a")
          tab.setAttribute("tabIndex","1");

        },}


    });
  </script>


</body>

</html>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。