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的更多相关文章
随机推荐
- NumPy的基本操作
1 简介 NumPy 是用于处理数组的 python 库,部分用 Python 编写,但是大多数需要快速计算的部分都是用 C 或 C ++ 编写的.它还拥有在线性代数.傅立叶变换和矩阵领域中工作的函数 ...
- 20210720 noip21
又是原题,写下题解吧 Median 首先时限有 2s(学校评测机太烂,加到 4s 了),可以放心地筛 \(1e7\) 个质数并算出 \(s_2\),然后问题变为类似滑动求中位数.发现 \(s_2\) ...
- element后端管理布局
<template> <el-container> <el-header> <Header></Header> <span class ...
- ysoserial CommonsColletions6分析
CC6的话是一条比较通用的链,在JAVA7和8版本都可以使用,而触发点也是通过LazyMap的get方法. TiedMapEntry#hashCode 在CC5中,通过的是TiedMapEntry的t ...
- JavaScript中的async/await详解
1.前言 async函数,也就是我们常说的async/await,是在ES2017(ES8)引入的新特性,主要目的是为了简化使用基于Promise的API时所需的语法.async和await关键字 ...
- 关于 CLAHE 的理解及实现
CLAHE CLAHE 是一种非常有效的直方图均衡算法, 目前网上已经有很多文章进行了说明, 这里说一下自己的理解. CLAHE是怎么来的 直方图均衡是一种简单快速的图像增强方法, 其原理和实现过程以 ...
- java web 项目中web.xml 详解
web.xml详述: web.xml 是每个Java web 项目的必备文件,又叫做部署描述符,Servlet规范中定义的,是web应用的配置文件. 概念: .部署描述符文件就像所有XML文件一样,必 ...
- Apache Dolphin Scheduler - Docker Compose 详解
Apache DolphinScheduler 是一个分布式去中心化,易扩展的可视化 DAG 工作流任务调度系统.简称 DS,包括 Web 及若干服务,它依赖 PostgreSQL 和 Zookeep ...
- 【redis前传】集思广益之quicklist,取其精华去其糟粕
前言 在之前我们已经学习了redis五大数据结构中的list结构.其内部是linkedList和zipList两种结构.这是我们已经学习的内容.之前我没有结合操作具体查看.事实上在两者中还存在一种结合 ...
- Jmeter系列(12)- 上传接口压测
step-1上传接口分析 上传接口源码分析:分析上传文件类型.有无大小限制.存放上传文件服务器 没有源码通过抓包工具,或者Chrome查看框架源代码 接口路径/uploadfile,接口请求POST, ...