【问题标题】:How to show and hide each item of the array in ona HTML element?如何在 HTML 元素中显示和隐藏数组的每个项目?
【发布时间】:2019-11-13 13:22:28
【问题描述】:

我有这个数组:

   let learning = [
      "HTML",
      "CSS",
      "JavaScript",
      "JQuery",
      "Git",
      "GitHub",
      "Bootstrap",
      "Sass",
      "JSon"
   ];

并希望在 id="learning-here"

的 span 元素中一个一个地显示它们

我试过这样:

   for (const iterator of learning) {
      setTimeout(writeList(iterator), 1000);
   }

   function writeList(i) {
      $("#learning-here").text(i);
      console.log(i); 
   }

似乎 setTimeout 不像我想的那样工作。一旦我重新加载页面并且整个数组被控制台记录,数组上的最后一项就会显示在 span 上。

还尝试使用我的 JQuery 知识,如下所示:

   $.each(learning, function(key, value) {
      setTimeout(writeList, 1000);

      function writeList() {
         $("#learning-here").text(value);
      }
      console.log(value);
   });

如何在 span 上一一显示数组元素? 我在上面的两次尝试中做错了什么?

谢谢。

【问题讨论】:

  • 重复设置.text() 只是意味着您每次都覆盖相同的跨度。你的超时时间都是 1 秒。所以他们都同时开火,最后一个完成将显示在跨度中。尝试不同的超时以查看跨度每秒的变化。 (乘以索引?)

标签: javascript jquery html arrays


【解决方案1】:

setTimeout 函数可能不是这里的最佳选择。我认为你应该使用函数setInterval,它以一定的间隔运行特定的回调/函数,直到间隔被清除。

let learning = [
  "HTML",
  "CSS",
  "JavaScript",
  "JQuery",
  "Git",
  "GitHub",
  "Bootstrap",
  "Sass",
  "JSon"
];

// the index to show 
let currentIndex = 0;
let learningElement = document.getElementById('learning-here');

// we create an interval variable to clear it later,
// we are also using an arrow function to have access to variable outside the function's scope.
let interval = setInterval(() => {
  // we show the string at the current index and then increment the index.
  learningElement.innerHTML = learning[currentIndex++];
  // if the index if bigger than the length of our array, we've shown every element in the array.
  if(currentIndex >= learning.length) {
    // we clear the interval so it stops.
    clearInterval(interval);
    
    // alternativaly, we could start over by setting the current index back to 0
    //currentIndex = 0;
  }
}, 1000);
<div id="learning-here"></div>

因为你说你的 Jquery 知识有限,所以我选择使用纯 Javascript。

【讨论】:

    【解决方案2】:

    由于您所有的超时时间都是 1 秒,因此没有时间真正看到单词的变化。 尝试将超时时间与索引相乘,以便单词每 x 秒更改一次,直到到达最后一个单词。

    let learning = [
      "HTML",
      "CSS",
      "JavaScript",
      "JQuery",
      "Git",
      "GitHub",
      "Bootstrap",
      "Sass",
      "JSon"
    ];
    learning.forEach(( word, index ) => {
      setTimeout(() => {
        document.querySelector( '#learning-here' ).innerText = word;
      }, index * 2000 );
    });
    <span id="learning-here"></span>

    【讨论】:

      【解决方案3】:

      您的方法存在一些问题:

      1) setTimeout 需要一个函数的reference,它可以稍后执行 - 而writeList(iterator) 导致该函数立即执行,然后 result传递给 setTimeout,由于结果不是函数,所以没有用。

      2) 您正在同时关闭所有计时器(因为循环会立即运行,它不会等待循环内声明的计时器在再次循环之前触发)。因此它们也都同时完成,所以你只看到最后一个的结果。因此,如果您想要 1 秒的延迟,则需要增加每次迭代的超时时间。

      这里有一种方法可以让它发挥作用。不过,可能有几种不同的方法可以实现它。

      let learning = [
        "HTML",
        "CSS",
        "JavaScript",
        "JQuery",
        "Git",
        "GitHub",
        "Bootstrap",
        "Sass",
        "JSon"
      ];
      
      var timer = 0;
      $.each(learning, function(key, value) {
        timer = timer + 1000;
        setTimeout(function() {
          writeList(value)
        }, timer);
      });
      
      function writeList(val) {
        $("#learning-here").text(val);
      }
      <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
      <span id="learning-here"></span>

      【讨论】:

        【解决方案4】:

        这是一个简单的函数,它将递归运行,直到它遍历所有单词

        let learning = [
          "HTML",
          "CSS",
          "JavaScript",
          "JQuery",
          "Git",
          "GitHub",
          "Bootstrap",
          "Sass",
          "JSon"
        ];
        
        var i = 0;
        
        function writeList() {
          $("#learning-here").text(learning[i]);
          i++;
          if (i < learning.length) {
            setTimeout(writeList, 1000);
          }
        }
        writeList();
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
        <span id="learning-here"></span>

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-04-06
          • 1970-01-01
          • 1970-01-01
          • 2020-02-02
          • 2023-03-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多