Javascript显示和隐藏功能

Javascript show and hide function

本文关键字:功能 隐藏 显示 Javascript      更新时间:2023-09-26

我正在尝试使用一些容器和文本执行显示/隐藏功能。当我单击某个容器时,我希望隐藏一个段落。目前,当我单击"left"容器时,我正在尝试解决该段落"barbertext"消失。

<p class="hairtext" style="color:red">We are Thairapy, an independent hair <br> and beauty Salon located in the heart<br> of Exeter. At Thairapy, we care about<br> our clients and our main aim is to go<br> that extra mile and look after every <br> one of our happy customers.</p>
    <p class="beautytext" style="color:red">Our beautician, Shail Dutt has over<br> 10 years of experience within the beauty<br> industry. Shail is a very dedicated and<br> passionate person, she strives to make<br> her clients feel loved and special. </p>
    <p class="barbertext" style="color:red">A decent Mens haircut needn't cost the<br>Earth. We offer top quality Men's haircuts<br> from £7. </p>
    <div class="container" id= "left" >
        <h1 style="color:white"><a>HAIR</a></h1>
    </div>
    <div class= "container" id= "center">
        <h1 style="color:white"><a>BEAUTY<a/></h1>
    </div>
    <div class="container" id= "right">
        <h1 style="color:white"><a>BARBERS</a></h1>
    </div>
</div>

我正在使用的Javascript是这样的:

<script>
$(document).ready(function(){
  $("#hairtext").click(function(){
    $("barbertext").hide();
  });
  $("#hairtext").click(function(){
    $("barbertext").show();
  });
});
</script>

如果有人能帮忙,我将不胜感激。

将两个处理程序绑定到同一元素,因此每次单击时都会隐藏和显示。

尝试toggle(),使其在隐藏/显示之间交替

$(document).ready(function(){
  $(".hairtext").click(function(){
    $(".barbertext").toggle();
  });
});

需要将.添加到理发文本选择器,并为发型文本添加.#用于 ID

我做了以下小提琴,应该可以满足您的需求 http://jsfiddle.net/Lyd9hgh2/

$(document).ready(function(){
    var hidden= false;
  $("#left").click(function(){
      if(hidden){
          $(".barbertext").show();
          hidden = false;
      }else
      {
          $(".barbertext").hide();
          hidden = true;
      }
  });
});

你的选择器做错了:

$(".barbertext")

我不确定你想要什么,但当用户单击容器时,似乎你想要,显示/隐藏一个关联的div。我为此创建了一个小提琴:http://jsfiddle.net/BenoitNgo/0yL02nop/6/

更正了现有代码:(根据您的需要 - 当我单击"左"容器时,"理发文本"段落消失)

<script>
    $(document).ready(function(){
      $("#left").click(function(){
        $(".barbertext").hide();
      });
      $("#left").click(function(){
        $(".barbertext").show();
      });
    });
    </script>

改进:

  1. 您可以将 Id 用于容器和段落以提高性能
  2. 如果你想
  3. 在容器单击时显示/隐藏行为,你可以使用 jquery 切换函数