HTML 点击按钮 删除或显示元素

第一种,隐藏,即不可见。元素仍然存在,所以结构不会改变。

<!DOCTYPE html>
<html>
<head>
    <script type="text/javascript">
        function removeElement()
        {
            if (document.getElementById("p1").style.visibility=="visible")
            {
                document.getElementById("p1").style.visibility="hidden";
            }
            else {
                document.getElementById("p1").style.visibility="visible";
            }
        }
    </script>
</head>
<body>

<h1>Hello</h1>

<p id="p1" style="visibility: hidden;">This is some text. This is some text. This is some text.
    This is some text. This is some text. This is some text.
    This is some text. This is some text. This is some text.</p>

<input type="button" onclick="removeElement()"
       value="Do not display paragraph" />

</body>
</html>

第二种,元素不显示,可能改变结构。

<!DOCTYPE html>
<html>
<head>
    <script type="text/javascript">
        function removeElement()
        {
            if(document.getElementById("p1").style.display=="none")
            document.getElementById("p1").style.display="inherit";
            else
                document.getElementById("p1").style.display="none";
        }
    </script>
</head>
<body>

<h1>Hello</h1>

<p id="p1">This is some text. This is some text. This is some text.
    This is some text. This is some text. This is some text.
    This is some text. This is some text. This is some text.</p>

<input type="button" onclick="removeElement()"
       value="Do not display paragraph" />

</body>
</html>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 13,826评论 1 92
  • 女士们,先生们:飞机正在下降。请您回原位坐好,系好安全带, 收起小桌板,将座椅靠背调整到正常位置...... 赵四...
    二木三皮阅读 341评论 0 0
  • 我的github:https://github.com/fcott19/XformerRecyclerView 最...
    fcott阅读 377评论 0 0
  • 我多么想 伫立岸边,眺望思念的桅杆,凝眸远方的白帆; 我...
    红尘修行阅读 193评论 0 0