基本上,这是一个店面网站。我希望能够在客户未登录时将价格显示为“登录获取费用”。未登录时,所有商品的价格默认为“$0.00”。
除了将价格更改为“登录查看费用”之外,我还想隐藏包含“添加到购物车”按钮和比较按钮的 div。
我关注了example (在“添加 Javascript”下找到)我在网上找到的,这就是我到目前为止想到的..
<script type="text/javascript">
$(document).ready( function() {
$('.p-price em:contains("$0.00")').text("Login for Cost");
$('.p-price:contains("$0.00")').text("Login for Cost");
})
</script>
<script type="text/javascript">
$(document).ready( function() {
if ($('.p-price').html()=='Login for Cost') {
$('.ProductActionAdd').hide();
$('.ProductCompareButton').hide();
$('.CompareButton').hide();
} else {
$('.ProductActionAdd').show();
$('.ProductCompareButton').show();
$('.CompareButton').show();
}
})
</script>
第一个脚本有效,未登录时价格将更改为“登录支付费用”,并在登录时显示实际价格。但是,第二个脚本在登录和未登录时隐藏 3 个 div .
感谢任何帮助!提前致谢!
最佳答案
在 JavaScript 中使用 ==
是一种不好的做法。相反,请像下面这样使用 ===
并查看它是否有效。并且还使用 .text()
而不是 .html()
:
<script type="text/javascript">
$(document).ready( function() {
if ($('.p-price').text()==='Login for Cost') {
//Your code to hide
} else{
//Your code to show
})
</script>
关于javascript - 如果另一个 div 具有特定文本,则隐藏 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38493427/