当前位置:编程学习 > JS >>

求一个JS控制DIV显示隐藏的代码

求一个完整的JS+DIV代码。如图所示,Open和Close为文字,不是按钮 就是要默认隐藏……我试了几回,打开页面的时候都是展开的。 http://hiphotos.zhaoxi.net/opriya/pic/item/7e3c06f9deeacb0dddc474b3.jpg
追问:非常感谢,效果应该是没错,但是代码好像有点问题?我这不能正常显示效果,点了open没有反应,麻烦检查一下代码之前没试清楚不好意思,直接作为HTML页面确实没有问题,但是我想用blog之类的程式(支持html)直接作为日志发表好像不行,是不是哪里还得修改?
答案:1楼的可能误会楼主的意思了吧?
我的理解,应该是这效果吧?(如有问题,请继续追问)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>

<body>
  <div style="width:200px; padding:10px; font-weight:bold; font-size:14px; line-height:18px; background:#666666;">
    <div>Messages</div>
    <div><input style="border:0px; font-size:14px; font-weight:bold; background:#666666; cursor:pointer;" onFocus="this.blur();" onclick="return Switch(this);" value="Open..." readonly></div>
    <div id="HiddenMsg" style="display:none;">隐藏信息...<br>隐藏信息...<br>隐藏信息...<br>隐藏信息...<br>隐藏信息...</div>
  </div>
<script type="text/javascript">
function Switch(Str){
    if(Str.value.indexOf("Open") >= 0){
        Str.value = "Close...";
    }else{
        Str.value = "Open...";
    }

    var MsgStyle = document.getElementById("HiddenMsg").style;
    if(MsgStyle.display == "block"){
        MsgStyle.display = "none";
    }else{
        MsgStyle.display = "block";
    }
}
</script>
</body>
</html>
其他:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<style type="text/css">
	.div1{ width:300px; height:80px; border:1px solid green;}
	.div2{ width:300px; height:80px; border:1px solid red;}
</style>
<script type="text/javascript">
	function init(){
		document.getElementById("div1").style.display="none";
		document.getElementById("div2").style.display="none";
	}
</script>
</head>

<body onload="init()">
<center>
	<div id="div1" class="div1">Open</div>
	<div id="div2" class="div2">Close</div>
</center>
</body>
</html> 用js控制层的style的display的none和block值就可以实现了。 

上一个:用什么源码能实现如图中的功能,js可以不,怎么实现,急!!!
下一个:<script src="/{maxcms:sitepath}js/common.js"></script>

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,