我有以下 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/