一个奇怪的有关层的隐藏和显示的问题?

来源:百度知道 编辑:UC知道 时间:2024/06/16 09:12:34
示例代码如下,为什么每次第一次加载的时候点击无效
<html>
<head><title>test</title></head>
<style type="text/css">
body{
font-size:12px
}
.lantk{
display:none;
border:1px solid;
width:300px;
height:100px;
text-align:center;
padding-top:10%
}
.link{

width:130px;
padding:10px;
border:1 #000 solid;
}
</style>
<script type="text/javascript">
function showHideDIV(){
var doc = document.getElementById("Maple");
doc.style.display=doc.style.display=="none"?"block":"none";

}
</script>
<body>
<div><a href="#none" class="link" onclick="showHideDIV()">点击我显示或隐藏层</a></div>
<hr />
<div id="Maple" class="Lantk">
<pre>
测试用例

因为Maple元素的style.display定义在class里,没有定义在自己那里,下面这样就可以了:
<div id="Maple" class="Lantk" style="display:none;">

你可以测试一下:
function showHideDIV(){
var doc = document.getElementById("Maple");
alert(doc.style.display);