折叠/扩展文本 - 逻辑错误

时间:2013-02-03 16:12:58

标签: javascript html

我有这个脚本,当我点击一个按钮时会执行。单击该按钮时,下面的文本将折叠,当我再次单击该按钮时,文本将被展开并查看。

但是,我想要的是相反的。当页面自动加载时我想隐藏/折叠文本,只有在点击按钮时才会显示。

我该如何改变?

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>

3 个答案:

答案 0 :(得分:1)

更改

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

这有用吗?

答案 1 :(得分:1)

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

window.onload = function() {

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

}

或者您可以输入您的HTML

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

答案 2 :(得分: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>
相关问题