首页 > 解决方案 > 其中一段被函数的输出替换

问题描述

在下面给出的代码中,一旦我们单击按钮,该段落就会被删除。为什么?

<html>
<head>
    <script>
        function func_1()
        {
            document.getElementById("show").innerHTML = "Hello World";
        }
    </script>
</head>
<body>
    <h1>First</h1>
    <button onclick="func_1()">Click here </button>

    <p id="show"> Second</p>
</body>
</html>

标签: javascripthtml

解决方案


innerHTML 将所选 div 的内容替换为您提供给它的内容。因此,在您的情况下,删除“第二”并添加“Hello world”


推荐阅读