如果另一个 div 有特定文本,则隐藏 divs

Hide divs if another div has certain text

基本上,这是一个店面网站。我希望能够在客户未登录时将价格显示为 "Login For Cost"。未登录时,所有商品的价格默认为“$0.00”。

在将价格更改为 "Login For Cost" 的同时,我想隐藏包含 "Add to cart" 按钮和比较按钮的 div。

我在网上找到了 example(在 "Adding the Javascript" 下找到),这就是我到目前为止想出的..

<script type="text/javascript">
$(document).ready( function() {
  $('.p-price em:contains("[=11=].00")').text("Login for Cost");
  $('.p-price:contains("[=11=].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>

第一个脚本有效,未登录时价格将更改为 "Login for Cost",并在登录时显示实际价格。但是,第二个脚本在登录和未登录时都隐藏了 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>