2013-02-03 56 views
1

我有这个脚本,当我点击一个按钮时会被执行。当按钮被点击时,下面的文本将折叠,当我再次点击按钮时,文本将被展开和查看。崩溃/展开文本 - 逻辑错误

但是,我想要的是相反的。当页面加载它时,我想隐藏/折叠文本,并且只在点击按钮时显示。

我该如何改变?

JS

function toggleMe(a) { 
       var e = document.getElementById(a); 
       if (!e) 
        return true; 
       if (e.style.display == "none") { 
        e.style.display = "block" 
       } else { 
        e.style.display = "none" 
       } 
       return true; 
      } 

HTML

<input type="button" ="return toggleMe('para1')" value="Toggle"> 
       <br> 
       <p id="para1"> 
        some text................ </p> 

回答

0
<html> 
<head> 
<script> 
function toggleMe(a) { 
       var e = document.getElementById(a); 
       if (!e) 
        return true; 
       if (e.style.display == "none") { 
        e.style.display = "block" 
       } else { 
        e.style.display = "none" 
       } 
       return true; 
      } 
</script> 

<body> 
<input type="button" onclick="toggleMe('para1')" value="Toggle"> 
       <br> 
       <p id="para1" style="display:none;"> 
        some text................ </p> 

</body> 
</html> 
+0

我只是想隐藏的所有文本的onLoad,并单击按钮时显示的文字。但在你的方法中,当我点击按钮时,我无法显示文本。 –

+0

粘贴的工作代码 – user2031802

+0

什么都没有,与之前一样:( –

1

变化

 <p id="para1"> 
to 
     <p id="para1" style="display: none"> 

这是否起作用?

+0

我只想隐藏所有文本onLoad,并单击按钮时显示文本。但在您的方法中,我无法显示当我点击按钮时的文本 –

1

为什么你不隐藏窗口加载文本?

window.onload = function() { 

    document.getElementById("para1").style.display = "none"; 

} 

,也可以键入到HTML

<p id="para1" style="display: none"> 
       some text................ 
</p> 
+0

它消失了,但我没有看到当我点击按钮时显示的文本 –