Javascript 显示和隐藏函数

标签 javascript jquery html show-hide

我正在尝试对一些容器和文本执行显示/隐藏功能。 当我点击某个容器时,我想隐藏一段。目前我正在尝试解决当我单击 "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>

如果有人能提供帮助,我将不胜感激。

最佳答案

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

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

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

(需要添加 . 到 barbertext 选择器,. 用于头发文本。# 用于 ids)

关于Javascript 显示和隐藏函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28139861/

相关文章:

javascript - 属于实例的对象是否应该与原型(prototype)保持链接?

javascript - 使用ajax插入数据库,500(内部服务器错误)

html - 从 angular 4 中的 html 中删除主机组件标签

javascript - 模态不显示在 vue.js 中

javascript - Html 包含 jQuery

javascript - 我如何编写一个函数,如果它的参数是一个 Node 集合,则对 javascript 中的所有 Node 元素执行?

javascript - SVG 无法正确呈现为主干 View

javascript - 在带有 scroll-x : auto not working in iOS & Safari 的容器内带有点击贴图的图像

javascript - 有没有办法在回调中保留此引用?

php - 将 bool 值转换为整数