【问题标题】:How to control the span characteristics based on given data如何根据给定数据控制跨度特性
【发布时间】:2021-02-20 06:41:48
【问题描述】:

以下 HTML 代码创建 3 个元素,并允许用户单击/选择它们。

const changeColor = (evt) => {
  if (evt.currentTarget.classList.contains("is-active")) {
    evt.currentTarget.classList.remove("is-active");
  } else {
    evt.currentTarget.classList.add("is-active");
  }
};
const EL_tagger1010_children = document.querySelectorAll(".tagger1010 span");
EL_tagger1010_children.forEach(EL => EL.addEventListener("click", changeColor));
.tagger1010 span {
  padding: 6px 10px;
  background: #D0E8E4;
  border-radius: 18px;
  color: #000000;
  font-family: Roboto;
  font-size: 12px;
  margin: 0 4px 8px 0;
  font-weight: 500;
  display: inline-block;
  word-wrap: break-word;
  white-space: normal;
  cursor: pointer;
  user-select: none;
  border: 1px solid BBD0CD;
}

.tagger1010 span.is-active {
  background-color: #008fde;
  color: #ffffff;
}

.tagger1010 span:hover {
  background-color: #008fde;
  color: #ffffff;
}
<div class="tagger1010">
  <span>Google</span>
  <span>Microsoft</span>
  <span>Facebook</span>
  <span>LinkedIn</span>
</div>

<div class="as-console-wrapper"></div>
<div class="as-console"></div>

如果标签包含在给定列表中,我希望将跨度预分配为“处于活动状态”。

例如,如果您运行上面的代码,并且给定的列表包括“Microsoft”和“LinkedIn” - 我希望“Microsoft”和“LinkedIn”已经被突出显示并且背景颜色为 #008fde ,颜色为#ffffff。

有谁知道我怎么说,“如果这个 span 的文本包含在这个列表中,让它具有 is-active 特征”

【问题讨论】:

  • 您可以获取所有需要的元素(如 tagger1010 的所有子元素或所有跨度)并检查每个元素以查看其文本是否包含在某个预定义列表中。如果是添加 .is-active 类

标签: javascript html


【解决方案1】:

在这里结帐 https://jsfiddle.net/qmx3105s/

<script type="text/javascript">
                const changeColor = (evt) => {
                    if (evt.currentTarget.classList.contains("is-active")){
                            evt.currentTarget.classList.remove("is-active");
                            localStorage.removeItem(evt.currentTarget.textContent);
                            
                     } else {
                          evt.currentTarget.classList.add("is-active");
                        localStorage.setItem(evt.currentTarget.textContent,'true');
                    }
                };
                const EL_tagger1010_children = document.querySelectorAll(".tagger1010 span");
                EL_tagger1010_children.forEach(EL => {
                    console.log('EL',EL)
                    if(localStorage.getItem(EL.textContent)){
                        EL.classList.add("is-active");
                    }
                    EL.addEventListener("click", changeColor);
                });
        </script>

【讨论】:

  • @youewew 用你的脚本替换上面的脚本。
【解决方案2】:

已编辑:Search by innerTextFind in Array 添加。

原文:我强烈建议将id="X" 添加到您的html 中,以便更轻松地定位特定标记。不得不依赖内部文本要复杂得多,也是不好的做法。

然后您需要一个数组来保存您的 ID 并对其进行迭代。最后我们添加.is-active

看起来是这样的:

const changeColor = (evt) => {
    if (evt.currentTarget.classList.contains("is-active")) {
        evt.currentTarget.classList.remove("is-active");
    } else {
        evt.currentTarget.classList.add("is-active");
    }
};
const EL_tagger1010_children = document.querySelectorAll(".tagger1010 span");
EL_tagger1010_children.forEach(EL => EL.addEventListener("click", changeColor));

var tag_names = ["Microsoft", "LinkedIn"];
var tags = document.getElementsByTagName("span");

for (var i = 0; i < tags.length; i++) {
    if(tag_names.indexOf( tags[i].textContent ) != -1){
       tags[i].classList.add('is-active');
    }
}
.tagger1010 span {
  padding: 6px 10px;
  background: #D0E8E4;
  border-radius: 18px;
  color: #000000;
  font-family: Roboto;
  font-size: 12px;
  margin: 0 4px 8px 0;
  font-weight: 500;
  display: inline-block;
  word-wrap: break-word;
  white-space: normal;
  cursor: pointer;
  user-select: none;
  border: 1px solid BBD0CD;
}

.tagger1010 span.is-active {
  background-color: #008fde;
  color: #ffffff;
}

.tagger1010 span:hover {
  background-color: #008fde;
  color: #ffffff;
}
<div class="tagger1010">
  <span>Google</span>
  <span>Microsoft</span>
  <span>Facebook</span>
  <span>LinkedIn</span>
</div>

<div class="as-console-wrapper"></div>
<div class="as-console"></div>

【讨论】:

  • 我认为这不是解决方案,因为它是静态的。当您刷新页面时。它将重新开始。
  • OP 对此没有任何要求。仅突出显示列表:pre-assign spans as "is-active" if the tag is included in a given list.
  • @Miro 谢谢!我无法实现 id 的想法——虽然它是一个很棒的想法。我正在使用 Bubble.io 平台,我无法动态获取计数以将 id 与列表中的计数相关联。该列表不会超过 30-40 个元素。什么会使获取内部文本成为不好的做法?我认为它要慢得多。
  • 我明白了。立即检查。
猜你喜欢
  • 1970-01-01
  • 2023-02-22
  • 2014-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-21
  • 1970-01-01
  • 2015-07-05
相关资源
最近更新 更多