javascript innerHTML如果classList包含此内容

时间:2019-03-07 18:12:22

标签: javascript innerhtml

我要编辑一个段落元素-

<p id="individualDescription"></p>
在我的HTML上

取决于哪个幻灯片在我的html中处于活动状态。我正在使用引导轮播,该类会将活动类传递给当前显示的图片。

我当前正在使用classList.contains来检查元素是否具有要在段落中编写的类和innerHTML。

JS:

var slideIndex1 = document.querySelector('#firstSlide');
var slideIndex2 = document.querySelector('#secondSlide');
var slideIndex3 = document.querySelector('#thirdSlide');
var slideIndex4 = document.querySelector('#fourthSlide');
var workoutDescription = document.querySelector('#individualDescription');

if (slideIndex1.classList.contains('active')) {
  workoutDescription.innerHTML = 'This is Slide 1';
} else if (slideIndex2.classList.contains('active')) {
  workoutDescription.innerHTML = 'This is Slide 2';
} else if (slideIndex3.classList.contains('active')) {
  workoutDescription.innerHTML = 'This is Slide 3';
} else if (slideIndex4.classList.contains('active')) {
  workoutDescription.innerHTML = 'This is Slide 4';
};

HTML:

<div id="carouselExampleIndicators" class="carousel slide" data-ride="carousel">
      <ol class="carousel-indicators">
        <li data-target="#carouselExampleIndicators" data-slide-to="0" id="firstSlide" class="active"></li>
        <li data-target="#carouselExampleIndicators" data-slide-to="1" id="secondSlide"></li>
        <li data-target="#carouselExampleIndicators" data-slide-to="2" id="thirdSlide"></li>
        <li data-target="#carouselExampleIndicators" data-slide-to="3" id="fourthSlide"></li>
      </ol>
      <div class="carousel-inner">
        <div class="carousel-item active">
          <img class="d-block w-100" src="../Images/IW_1.jpg" alt="First slide">
        </div>
        <div class="carousel-item">
          <img class="d-block w-100" src="../Images/IW_2.jpg" alt="Second slide">
        </div>
        <div class="carousel-item">
          <img class="d-block w-100" src="../Images/IW_3.jpg" alt="Third slide">
        </div>
        <div class="carousel-item">
          <img class="d-block w-100" src="../Images/IW_4.jpg" alt="Fourth slide">
        </div>
      </div>
      <a class="carousel-control-prev" href="#carouselExampleIndicators" role="button" data-slide="prev">
        <span class="carousel-control-prev-icon" aria-hidden="true"></span>
        <span class="sr-only">Previous</span>
      </a>
      <a class="carousel-control-next" href="#carouselExampleIndicators" role="button" data-slide="next">
        <span class="carousel-control-next-icon" aria-hidden="true"></span>
        <span class="sr-only">Next</span>
      </a>
    </div>

这是幻灯片的代码。它可以完全正常工作,所以这里没有问题。

我不确定为什么我要显示的文本没有显示出来-段落元素保持空白并且什么都没有改变。

1 个答案:

答案 0 :(得分:0)

如果您正在使用Bootstrap的轮播,可以安全地假设您正在使用JQUERY。在这种情况下,您可以使用活动类找到该项目的索引并使用开关。

$(document).ready(function(){
var msg = "";

switch($(".carousel-item.active").index()){
    case 1: msg = "test1";break;
    case 2: msg = "test2";break;
    case 3: msg = "test3";break;
}
$("#individualDescription").html(msg);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="carousel-item active"></div>
<div class="carousel-item"></div>
<div class="carousel-item"></div>

<p id="individualDescription"></p>