使用javascript函数显示div

Bru*_*uno 1 html javascript

我想在用户点击按钮时在网页上显示div.

有人知道怎么做吗?

到目前为止,我的代码是:

        <html>
        <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso 8859-1" />
        </head>
        <body>
          <input id="text" type="text" size="60" value="Type your text here" />
          <input type="button" value="When typing whatever text display the div balise on the page" onclick="check();" />

          <script type="text/javascript">

        function check() {
              //Display my div balise named level0;
        }

          </script>

        </body>
      </html>
Run Code Online (Sandbox Code Playgroud)

谢谢,

布鲁诺

编辑:我的所有代码(我删除了它,因为它太长而且不太清楚)

Tik*_*vis 5

你可以document.createElement("div")用来实际制作div.然后,您可以使用innerHTML文本填充div .之后,使用将其添加到身体appendChild.总而言之,它看起来像这样:

function check() {
    var div = document.createElement("div");
    div.innerHTML = document.getElementById("text").value;
    document.body.appendChild(div);
}
Run Code Online (Sandbox Code Playgroud)

每按一次按钮,这将添加一个div.如果你想每次都更新div,你可以div在函数外面声明变量:

var div;
function check() {
    if (!div) {
        div = document.createElement("div");
        document.body.appendChild(div);
    }

    div.innerHTML = document.getElementById("text").value;
}
Run Code Online (Sandbox Code Playgroud)

如果页面中已经有id为"level0"的div,请尝试:

function check() {
    var div = document.getElementById("level0");
    div.innerHTML = document.getElementById("text").value;
}
Run Code Online (Sandbox Code Playgroud)