【前端小技巧】用CSS隐藏元素的几种方法

用 CSS 隐藏元素有很多种方法,这里介绍 3 种常见的。

opacity: 0

特点是【看不见,占空间,摸得着】

  • 元素隐藏

  • 不改变布局

  • 如果绑定了事件,点击该区域,可以触发事件的

visibility: hidden

特点是【看不见,占空间,摸不着】

  • 元素隐藏

  • 不改变布局

  • 如果绑定了事件,点击该区域,无法触发事件的

display: none

特点是【看不见,不占空间,摸不着】

  • 元素隐藏

  • 改变布局

  • 如果绑定了事件,点击该区域,无法触发事件的

接下来,我们来编写代码验证一下。首先写入三个方块,对中间的橙色方块添加点击事件。代码及页面效果如下所示:

<!DOCTYPE html><html>    <head>        <style type="text/css">            .box {width: 200px;height: 50px;}            .red {background-color: red;}            .orange {background-color: orange;}            .yellow {background-color: yellow;}</style>    </head>
    <body>        <div>            <div class='box red'></div>            <div class='box orange' id="btn"></div>            <div class='box yellow'></div>        </div>        <script type="text/javascript">            document.getElementById("btn").onclick = function() {                alert('触发点击操作 0.0');            }</script>    </body></html>

opacity: 0

对中间橙色方块添加 opacity: 0 样式,代码及效果如下:

  • 元素隐藏

  • 不改变布局

  • 如果绑定了事件,点击该区域,可以触发事件的

<!DOCTYPE html><html>    <head>        <style type="text/css">            .box {width: 200px;height: 50px;}            .red {background-color: red;}            .orange {background-color: orange;}            .yellow {background-color: yellow;}            .opacity {opacity: 0}</style>    </head>
    <body>        <div>            <div class='box red'></div>            <div class='box orange opacity' id="btn"></div>            <div class='box yellow'></div>        </div>        <script type="text/javascript">            document.getElementById("btn").onclick = function() {                alert('触发点击操作 0.0');            }</script>    </body></html>

visibility: hidden

对中间橙色方块添加 visibility: hidden 样式,代码及效果如下:

  • 元素隐藏

  • 不改变布局

  • 如果绑定了事件,点击该区域,无法触发事件的

<!DOCTYPE html><html>    <head>        <style type="text/css">            .box {width: 200px;height: 50px;}            .red {background-color: red;}            .orange {background-color: orange;}            .yellow {background-color: yellow;}            .visibility {visibility: hidden}</style>    </head>
    <body>        <div>            <div class='box red'></div>            <div class='box orange visibility' id="btn"></div>            <div class='box yellow'></div>        </div>        <script type="text/javascript">            document.getElementById("btn").onclick = function() {                alert('触发点击操作 0.0');            }</script>    </body></html>

display: none

对中间橙色方块添加 display: none 样式,代码及效果如下:

  • 元素隐藏

  • 改变布局

  • 如果绑定了事件,点击该区域,无法触发事件的

<!DOCTYPE html><html>    <head>        <style type="text/css">            .box {width: 200px;height: 50px;}            .red {background-color: red;}            .orange {background-color: orange;}            .yellow {background-color: yellow;}            .display {display: none}</style>    </head>
    <body>        <div>            <div class='box red'></div>            <div class='box orange display' id="btn"></div>            <div class='box yellow'></div>        </div>        <script type="text/javascript">            document.getElementById("btn").onclick = function() {                alert('触发点击操作 0.0');            }</script>    </body></html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值