HTML如何做到点击按钮弹出一张图片?

html-css018

HTML如何做到点击按钮弹出一张图片?,第1张

这个可以使用js来帮助完成,给三个不同的按钮都绑定点击事件,对应另外三个div,每个div里面使用img标签引入一张图片(或者做成div的背景也行),三个div的外层再套一个div,用来做遮罩层,开始的时候所有div都隐藏,当点击某一个按钮的时候,遮罩层显示,里面对应的一个div显示,其他两个隐藏,这样就行了啊,还可以借助别人做好的框架,实现的方式很多哦

<!DOCTYPE HTML PUBLIC "-//W3C//DTDHTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<title>New Document </title>

<link rel="stylesheet" rev="stylesheet"href="style.css" type="text/css" />

<script type="text/javascript">

functionopenContent(x){

document.getElementById("content").style.display="block"

document.getElementById("content_img").getElementsByTagName("img")[0].src=document.getElementsByTagName("img")[x].alt

document.getElementById("content_img").style.display="block"

}

functioncloseContent(x){

document.getElementById("content").style.display="none"

x.parentNode.style.display="none"

}

</script>

<style type="text/css">

table{width:90%

height:90%

margin:auto

text-align:center

margin-top:20px}

#photo{margin:auto

width:1000px

height:678px

border:1pxsolid #ccc

margin:10px

float:left}

.photoTitle{height:30px}

#content{display:none

position:absolute

width:100%

height:100%

z-index:1

background:#000

top:0px

filter:alpha(opacity=85)}

#content_img{display:none

position:absolute

z-index:2

width:100%

text-align:center

top:100px

left:0px

}

</style>

</head>

<body>

<div id="top"><div id="txt">成功没有早晚<br/>&nbsp&nbsp&nbsp&nbsp努力就有收获</div>

</div>

<div id="nav">

<ul>

<li><ahref="index.html" target="_self">首页</a></li>

<liclass="bar">|</li>

<li><ahref="diary.html" target="_self">日志</a></li>

<liclass="bar">|</li>

<li><ahref="photo.html" target="_self">相册</a></li>

<liclass="bar">|</li>

<li><ahref="#" target="_self">留言</a></li>

</ul>

</div>

<div id="main">

<divid="photo">

<h4>|图片列表</h4>

<table>

<trclass="photoTitle"><td><ahref="javascript:openContent(0)">图片</a></td>

<td><ahref="javascript:openContent(1)">图片</a></td>

<td><ahref="javascript:openContent(2)">图片</a></td></tr>

<trvalign="top"><td><ahref="javascript:openContent(0)"><imgsrc="img/1.png" title="单击显示大图"alt="img/1.jpg"/></a></td>

<td><ahref="javascript:openContent(1)"><imgsrc="img/2.png" title="单击显示大图" alt="img/2.jpg"/></a></td>

<td><ahref="javascript:openContent(2)"><imgsrc="img/3.png" title="单击显示大图"alt="img/3.jpg"/></a></td></tr>

<trclass="photoTitle"><td><ahref="javascript:openContent(3)">图片</a></td>

<td><ahref="javascript:openContent(4)">图片</a></td>

<td><ahref="javascript:openContent(5)">图片</a></td></tr>

<trvalign="top"><td><ahref="javascript:openContent(3)"><imgsrc="img/3.png" title="单击显示大图"alt="img/3.jpg"/></a></td>

<td><ahref="javascript:openContent(4)"><img src="img/2.png"title="单击显示大图" alt="img/2.jpg"/></a></td>

<td><ahref="javascript:openContent(5)"><imgsrc="img/1.png" title="单击显示大图"alt="img/1.jpg"/></a></td></tr>

<trclass="photoTitle"><td><ahref="javascript:openContent(6)">图片</a></td>

<td><ahref="javascript:openContent(7)">图片</a></td>

<td><ahref="javascript:openContent(8)">图片</a></td></tr>

<trvalign="top"><td><ahref="javascript:openContent(6)"><imgsrc="img/2.png" title="单击显示大图"alt="img/2.jpg"/></a></td>

<td><ahref="javascript:openContent(7)"><imgsrc="img/1.png" title="单击显示大图"alt="img/1.jpg"/></a></td>

<td><ahref="javascript:openContent(8)"><imgsrc="img/3.png" title="单击显示大图"alt="img/3.jpg"/></a></td></tr>

</table>

</div>

</div>

<div id="content"></div>

<div id="content_img">

<imgsrc="" id="img_dis"/><br/>

<buttononclick="closeContent(this)" id="content_btn">关闭窗口</button>

</div>

<div id="bottom">

底部个人信息

</div>

</body>

</html>

效果如下:(至于再点击大图后效果消失,你可以把关闭窗口按钮触发的代码改成单击后的代码)