首页 > 解决方案 > 如何使用 JS 在 HTML 中定位特定的 span 标签

问题描述

嗨,我有一个带有几个 HTML 的 HTML,并且只有几个我想要淡化的 span 标签。

我选择了所有跨度:

const spans = document.querySelectorAll("span");
const animation = function () {
  for (let span of spans) span.classList.toggle("fade");
};
setInterval(animation, 2400);

现在我只想在 div 中定位这两个跨度

<div class="animated">
  Hey, I'm<br>
  <span
    class="color-primary mt-5 fade" 
    id="animated-name" 
    style="margin-top: 20px;"
  >
    Name Name
  </span>
  <span 
    class="color-primary mt-5" 
    id="animated-text" 
    style="margin-top: 20px;"
  >
    Text Text 
    <br> 
    Text & Text
  </span>
</div>

我的问题是如何仅针对这 2 个跨度标签来切换淡入淡出?

标签: javascripthtmlcss

解决方案


为什么不使用document.querySelectorAll('div.animated > span')


推荐阅读