JS实现“隐藏与显示”功能(多种方法)

本文介绍了三种使用JavaScript实现元素隐藏与显示的方法,包括通过按钮控制、时间限制控制和jQuery的slideToggle()方法。同时,文章还提到了nextSibling和previousSibling属性在页面元素操作中的应用。
摘要由CSDN通过智能技术生成

1,通过按钮实现隐藏与显示:

这个是通过按钮点击实现的隐藏与显示,具体代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<!DOCTYPE html>
<html>
<head>
<meta charset= "UTF-8" >
<title>通过按钮实现隐藏和显示</title>
<style type= "text/css" >
.body{
margin: 0 auto;
}
#show{
width: 600px;
height: auto;
font-size: 14px;
padding: 0px 0px 0px 5px; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; border-left: 3px solid rgb(108, 226, 108); line-height: 20px; width: 640px; clear: both; outline: 0px !important; border-radius: 0px !important; border-top: 0px !important; border-right: 0px !important; border-bottom: 0px !important; border-image: initial !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; box-sizing: content-box !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; min-height: auto !important; color: gray !important;">#E4C392;
display: block;
padding: 10px;
margin: 0 auto;
border-radius: 10px;
}
#show h2{
color: #11C2EE;
margin: 0 auto;
}
.slide{
margin: 0 auto;
padding: 0;
width: 600px;
border-top: solid 4px #D6A55C;
}
.btn-slide{
 
width: 80px;
height:30px;
text-align: center;
margin: 0 auto;
border-radius: 8px;
margin: 0 auto;
display: block;
}
</style>
<script type= "text/javascript" >
function divShow(){
document.getElementById( "btnshow" ).style.display= "block" ;
document.getElementById( "btnhref" ).innerHTML = "关闭" ;
document.getElementById( "btnhref" ).href = "javascript:divhidden()" ;
}
function divhidden(){
document.getElementById( "btnshow" ).style.display= "none" ;
document.getElementById( "btnhref" ).innerHTML = "了解" ;
document.getElementById( "btnhref" ).href = "javascript:divShow()" ;
}
</script>
</head>
<body>
<div id= "show" >
<h2>通过点击按钮实现隐藏和显示</h2>
<hr />
<p>
欢迎来到我的博客Jaxzm!
</p>
<p>
目前我在学习GoF的设计模式,你想了解么?想要了解的话,请点击按钮。
</p>
<div id= "btnshow" style= "display: none;" >
<p>
GoF所描述的23种设计模式,总共可以分为三种类型:创建型模式,行为型模式,结构型模式。
</p>
<p>
我觉得比较难的是创建型模式,因为它说的比较抽象,所以我不容易理解它,然后我编码也比较少,所以就不太容易理解这个。
</p>
</div>
</div>
<p class= "slide" >
<a href= "javascript:divShow();" id=
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值