您的位置:首页 > Web前端 > JavaScript

JavaScript结合CSS样式实现可折叠区域的例子

2010-06-18 10:04 387 查看
这里介绍一个比较简单的办法,就是使用css的display属性。当display设置为none时,元素就会从页面流里被移除。

演示代码如下:

<html>
<head>
<title>Style Example</title>
<script type="text/javascript">
function toggle(sDivId) {
var oDiv = document.getElementById(sDivId);
oDiv.style.display = (oDiv.style.display == "none") ? "block" : "none";
}
</script>
</head>
<body>
<div style="background-color: blue; color: white; font-weight: bold; padding: 10px; cursor: pointer"
onclick="toggle('divContent1')">Click Here</div>
<div style="border: 3px solid blue; height: 100px; padding: 10px"
id="divContent1">This is some content
to show and hide.</div>
<p> </p>
<div style="background-color: blue; color: white; font-weight: bold; padding: 10px; cursor: pointer"
onclick="toggle('divContent2')">Click Here</div>
<div style="border: 3px solid blue; height: 100px; padding: 10px"
id="divContent2">This is some content
to show and hide.</div>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: