使用jQuery实现元素的显示隐藏

实现思路:通过jQuery的点击事件,对元素使用show( )、hide( )方法实现;也可以直接通过改变元素css样式实现。

1、首先得有个html结构的文件

复制代码
 <div class="tel">安全公告</div>
    <!-- 弹窗 -->
    <div class="mask">
      <h4>注意事项</h4>
      <p>
        1、注意电安全。不要乱动电器、乱接电线等,不要在标有高压危险的地方游荡。<br />
        2、注意水安全。不要到河边、水井等危险的地方玩耍。<br />
      </p>
      <div class="xz">
        <button type="button" id="no">取消</button>
        <button type="button" id="yes">同意</button>
      </div>
    </div>
复制代码

2、然后设置元素的相关样式使其看起来美观

复制代码
   <style>
      .tel {
        width: 100px;
        height: 40px;
        line-height: 40px;
        text-align: center;
        background-color: #e53d30;
        color: #fff;
        cursor: pointer;
      }
      .mask {
        width: 1000px;
        height: 800px;
        border: 1px solid #000;
        padding-left: 10px;
        text-align: center;
        position: absolute;
        top: 0;
        right: 0;
        left: 0;
        bottom: 0;
        margin: auto;
        display: none;
        z-index: 999;
      }
      .xz {
        position: absolute;
        bottom: 50px;
        left: 365px;
        width: 300px;
        height: 35px;
      }
      .xz button {
        width: 80px;
        height: 35px;
        border: none;
        color: #fff;
        cursor: pointer;
      }
      #no {
        background-color: #6e6c6c;
        float: left;
      }
      #yes {
        background-color: #e64c34;
        float: right;
      }
      p {
        text-align: left;
        line-height: 30px;
      }
   </style>
复制代码

3、最后对操作的元素设置事件即可

注意:使用以下方式的提前是已经引入jQuery文件

复制代码
 <script>
      $(".tel").click(function () {
        $(".mask").show();
        $(".tel").hide();
      });
      // 取消按钮
      $("#no").click(function () {
      // $(".mask").hide();
        $(".mask").css("display", "none");
      });
      //   确定按钮
      $("#yes").click(function () {
        $(".mask").hide();
        // 跳转到指定页面
        window.location.href = "http://index.html";
      });
  </script>
复制代码

 

posted @   Wanker  阅读(80)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示