javascript - 如果一个元素与另一个元素具有相同的值,则删除该元素

标签 javascript jquery

我正在寻找一种解决方案,如果 div 的跨度与另一个跨度具有相同的值,则隐藏该 div。

例如,在下面的代码中,我想隐藏 div.priceWithoutTax,以防 div.priceWithoutTax 范围与 div.salesPrice 范围具有相同的值

<div class="PriceBox" id="productPrice">
    <div class="ProdPriceBoxRedInner">
        <div class="salesPrice" style="display : block;">
            <span class="salesPrice">20,00 €</span>
        </div>
        <div class="PriceAdditional" style="top: 0px; display: none;">
            <div class="priceWithoutTax" style="display : block;">
                <span class="priceWithoutTax">20,00 €</span>
            </div>
        </div>
    </div>
</div>

提前致谢。

最佳答案

只需比较文本值,然后隐藏

var boxes = document.querySelectorAll('.PriceBox');

for (var i = boxes.length; i--;) {
    var wTax = boxes[i].querySelector('div.priceWithoutTax'),
        sale = boxes[i].querySelector('div.salesPrice');

    if ( wTax.textContent.trim() == sale.textContent.trim() )
        wTax.style.display = 'none';
}

FIDDLE

关于javascript - 如果一个元素与另一个元素具有相同的值,则删除该元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25889710/

相关文章:

javascript - react-router-dom: 无效的 hook 调用,Hooks 只能在函数组件的内部调用

javascript - 谷歌地图 API

javascript - Vuex store模块状态下如何访问 'this'

函数的 Javascript 类继承

jQuery : Replacing Fading with Sliding

javascript - 防止 Chrome 中链接点击时光标发生变化

javascript - 未捕获的类型错误 : Cannot read property 'top' of undefined error

javascript - 如何更改单击时按钮的样式并单击不同的按钮,应将其更改为默认值

javascript - 向数字添加逗号时出现问题

jquery - 带有下一个/上一个按钮的 Bootstrap 模态表单