JavaScript05
显示和隐藏
元素的显示和隐藏
元素display属性可控制元素的显示和隐藏,先获取元素对象,再通过点语法调用style对象中的display属性
语法格式:
元素.style.display='none'
属性值说明:
如果display值为"none"表示隐藏
如果display值为"block"表示显示
<!DOCTYPE html>
<html lang="en"> <head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>元素隐藏与显示</title>
<style>
div {
width: 200px;
height: 200px;
background-color: red;
display: block;
}
</style>
</head> <body>
<div id="red" style="display: block;"></div>
<script>
var red = document.getElementById('red');
// 对象.style 查看当前元素的css样式声明
// css样式声明也是一个对象 可以再通过对象.属性的方式查看具体的样式
console.log(red.style.display);
red.style.display = 'none';//隐藏元素
red.style.display = 'block';//显示元素
</script>
</body> </html>
案例:点击关闭按钮 隐藏图片
使用显示和隐藏属性关闭图片
图片的对齐方式
绑定关闭按钮事件,隐藏图片
<!DOCTYPE html>
<html lang="en"> <head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>元素隐藏与显示</title>
<style>
#red {
width: 200px;
height: 200px;
background-color: red;
display: block;
}
#img{
width:122px;
text-align: right;
position: fixed;
right: 0;
top:200px; }
</style>
</head> <body>
<div id="red" style="display: block;"></div>
<div id = "img">
<img id="target" src='img02-5.png' alt="">
<img id="colse" src='img01-5.png' alt="">
</div>
<script>
// 点击关闭按钮 关闭图片
// 找到页面元素 关闭的小图片和被关闭的大图片 保存到变量中
var target = document.getElementById('target')
var close = document.getElementById('close')
// 为小图片添加一个鼠标点击的事件
// 当小图片被点击后 将两个图片全部隐藏
close.onclic = function(){
target.style.display = "none";
close.style.display = 'none';
}
// 点击隐藏div img </script>
<script>
var red = document.getElementById('red');
// 对象.style 查看当前元素的css样式声明
// css样式声明也是一个对象 可以再通过对象.属性的方式查看具体的样式
console.log(red.style.display);
red.style.display = 'none';//隐藏元素
red.style.display = 'block';//显示元素
</script>
</body> </html>
<!DOCTYPE html>
<html lang="en"> <head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>对话框</title>
<style>
html,body{
height: 100%;
margin: 0%;
}
#bg {
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.3);
display: none;
}
#box{
width:300px;
height: 400px;
background-color: #fff;
border-radius: 5px;
position: fixed;
top:50%;
left: 50%;
margin-top: -200px;
margin-left: -150px;
display: none;
}
</style>
</head> <body>
<!-- 半透明的背景(遮罩层)-->
<div id="bg"></div>
<!-- 弹框(模态框)-->
<div id="box">
<span id="close">X</span>
</div>
<button id="open">显示弹窗</button>
<script>
// 获取页面中需要的元素
var open = document.getElementById('open')
var bg = document.getElementById('bg')
var box = document.getElementById('box')
var close = document.getElementById('close')
// 为open绑定点击事件
// 找到背景和弹框 将他们的属性display改为block
open.onclick=function(){
bg.style.display = 'block';
box.style.display = 'block';
}
// 为close绑定点击事件
// 找到背景和弹框 将他们的属性display改为none
close.onclick = function(){
bg.style.display = 'none';
box.style.display = 'none';
}
</script>
</body> </html>
JavaScript05的更多相关文章
随机推荐
- 常用ADB命令汇总
网络连接 通过TCP/IP连接设备 adb connect <ip:port> 断开已有的TCP/IP连接 adb disconnect <ip:port> 监听设备上指定的端 ...
- LeetCode通关:通过排序一次秒杀五道题,舒服!
刷题路线参考:https://github.com/chefyuan/algorithm-base 大家好,我是拿输出博客督促自己刷题的老三,前面学习了十大排序:万字长文|十大基本排序,一次搞定!,接 ...
- openwrt开发笔记二:树莓派刷openwrt
前言及准备 本笔记适用于第一次给树莓派刷openwrt系统的玩家,对刷机过程及注意事项进行了记录,刷机之后对openwrt进行一些简单配置. 使用openwrt源码制作固件需要花费一点时间. 平台环境 ...
- Python习题集(九)
每天一习题,提升Python不是问题!!有更简洁的写法请评论告知我! https://www.cnblogs.com/poloyy/category/1676599.html 题目 已知一个数列:1. ...
- Intel® QAT加速卡之加密、哈希操作流程和示例
Intel QAT 加密API介绍 文章主要讲述了Intel QAT 加密API接口的说明,以及多种应用场景下的使用方法. 文章目录 Intel QAT 加密API介绍 1. 概述 1.1 会话(se ...
- Openswan支持的算法及参数信息:
数据封装加密算法: algorithm ESP encrypt: id=2, name=ESP_DES, ivlen=8, keysizemin=64, keysizemax=64 algorithm ...
- Java基础之代理模式
代理模式是常见的设计模式之一,意图在为指定对象提供一种代理以控制对这个对象的访问.Java中的代理分为动态代理和静态代理,动态代理在Java中的应用比较广泛,比如Spring的AOP实现.远程RPC调 ...
- Mixed Content: The page at 'xxx' was loaded over HTTPS, but requested an insecure resource 'xxx'.
HTTPS页面里动态的引入HTTP资源,比如引入一个js文件,会被直接block掉的.在HTTPS页面里通过AJAX的方式请求HTTP资源,也会被直接block掉的. Mixed Content: T ...
- win10 移动端 android 测试环境搭建
一.移动端自动化测试的基础环境配置1:安装Java环境 关于安装Java环境以及相关环境变量的配置在我之前的博文分享中已有详细介绍,有需要的可以直接查找翻阅,这里就不再一一介绍. 二.移动端自动化测试 ...
- Windows系统中的SVN使用方法
Windows 下搭建 SVN(3.9版本)服务器 2018年08月11日 12:22:55 Amarao 阅读数 11984 版权声明:本文为博主原创文章,遵循CC 4.0 by-sa版权协议, ...