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

collapse/expand text - logic error

本文关键字:错误 文本 折叠      更新时间:2023-09-26

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

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

我怎样才能做出这种改变?

.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>

更改

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

这行得通吗?

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

window.onload = function() {
    document.getElementById("para1").style.display = "none";
}

或者你可以输入你的html

<p id="para1" style="display: none">
                some text................ 
</p>
<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>