javascript - 根据跨度类值切换 div

标签 javascript html getelementbyid

我有以下 block

<div id="discountText">
    <span>some text</span>
    <span class="discountType" style="display:none;">
</div>

installerDiscountType包含折扣类型(首选、已保存等)

如何显示 div万一<span class="discountType"等于“AAA”并隐藏它以防它等于其他东西?

我尝试了多种方法,但我不断得到未定义的值。

最佳答案

这将显示“Some Text AAA” 它显示“Some Text”,因为该范围没有类“discountType”

let types = document.querySelectorAll('.discountType')
types.forEach(node => {
  if(node.innerText !== "AAA") {
    node.style.display = 'none';
  }
})

<div id="discountText">
  <span>some text</span>
  <span class="discountType">Hello world</span>
  <span class="discountType">AAA</span>
</div>

关于javascript - 根据跨度类值切换 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57468913/

相关文章:

javascript - 通过 onclick 更改项目值不起作用

javascript - 通过函数参数传递 id

javascript - 将 "select"标签的值放入 JavaScript 变量中

javascript - React-Redux,调度函数时, "uncaught TypeError is not a function"

php - 如何使用 POST 将 javascript 对象数组传递给 php

javascript - 在 React Native 中调整图像大小

html - 获取缩放 div 以保持相同位置

html - Twitter 意图推文添加正斜杠

javascript - 为什么 props 在 render 和 componentWillReceiveProps 中显示不同的值?

html - Firefox 中无法选择的滚动条