<!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>
<script type="text/javascript">
function removeElement()
if(d7.style.display=="none")
{
d7.style.display=""//显示隐藏的DIV
}
else
{
d7.style.display="none"
}
</script>
</head>
<body>       
<div id=d7 style="display:none">http://www.sina.com.cn</div>
<a onclick="removeElement()">新浪网址</a>
</body>
</html>
为啥这功能不能实现啊,而我直接把<a onclick="
if(d7.style.display=='none')
{
d7.style.display=''
}
else
{
d7.style.display='none'
}"
>替换<a onclick="removeElement()">却能实现?搞不懂啊

解决方案 »

  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>
    <script type="text/javascript">
    function removeElement(){
    var obj = document.getElementById("d7");
    if(obj.style.display=="none") obj.style.display="";//显示隐藏的DIV
    else obj.style.display="none";
    }
    </script>
    </head>
    <body>       
    <div id=d7 style="display:none">http://www.sina.com.cn</div>
    <a onclick="removeElement()">新浪网址</a>
    </body>
    </html>
      

  2.   

    把javascript放到头部时,必须通过document来获取到d7,而不能直接获取,可以用document.getElementById("") 等方法获取。