引导4旋转木马上的hasclass不工作

a8jjtwal  于 2021-09-13  发布在  Java
关注(0)|答案(1)|浏览(325)

我正在用一个引导4转盘构建一个基本的窄播系统。一些幻灯片包含图像,其他幻灯片包含youtube视频。
当一个div包含一个youtube视频时,我想在幻灯片处于活动状态时自动播放,并在幻灯片处于非活动状态时停止播放。但不知何故,hasclass函数无法识别id=“video-1”的div上的“active”类。

  1. <script type="text/javascript">
  2. $( document ).ready(function() {
  3. console.log( "document loaded" ); // added for testing
  4. if ($("#video-1").hasClass('active')) {
  5. console.log( "video loaded" ); // added for testing
  6. $("#ytplayer-1")[0].src += "&autoplay=1";
  7. } else {
  8. $("#ytplayer-1")[0].src += "&autoplay=0";
  9. }
  10. });
  11. </script>

这是我的html:

  1. <div id="narrowcasting-carousel" class="carousel slide h-100" data-ride="carousel" data-interval="600000">
  2. <div class="carousel-inner h-100">
  3. <div class="carousel-item h-100 item-0" id="no-video" data-interval="10000">
  4. <img src="/images/image.png" />
  5. </div>
  6. </div>
  7. <div class="carousel-inner h-100">
  8. <div class="carousel-item h-100 item-1 active" id="video" data-interval="20000">
  9. <iframe id="ytplayer-1" width="100%" height="100%" src="https://www.youtube.com/embed/[CODE_HERE]?controls=0&amp;loop=0&amp;rel=0&amp;autoplay=0" frameborder="0" allowfullscreen="" allow="autoplay">
  10. </iframe>
  11. </div>
  12. </div>
  13. <div class="carousel-inner h-100">
  14. <div class="carousel-item h-100 item-2" id="no-video" data-interval="5000">
  15. <img src="/images/image.png" />
  16. </div>
  17. </div>
  18. </div>

当我使用onclick事件或击键事件时,它确实起作用。但这显然不是我需要的。

4ktjp1zp

4ktjp1zp1#

请尝试使用此代码

  1. <script type="text/javascript">
  2. $( document ).ready(function() {
  3. console.log( "document loaded" ); // added for testing
  4. if ($("iframe").parent().hasClass('active')) {
  5. console.log( "video loaded" ); // added for testing
  6. $("#ytplayer-1")[0].src += "&autoplay=1"; // You can refactor this line
  7. } else {
  8. $("#ytplayer-1")[0].src += "&autoplay=0";
  9. }
  10. });
  11. </script>

相关问题