我有這段代碼,它不能按我想要的方式工作。我希望它在每次單擊按鈕時在另一個下創建一個新的div,但它只是更改div的內容。幫助使用JavaScript/Dom和textarea創建div
<!DOCTYPE html>
<html>
<head>
<style>
p{
background-color: red;
font-family: arial;
}
</style>
</head>
<body>
Name: <textarea id="myText" value=""></textarea>
<button type="button" onclick="myFunction()">Enviar</button>
<div id="demo"></div>
<script>
function myFunction() {
var x = document.getElementById("myText");
var defaultVal = x.defaultValue;
var textoComentario = x.value;
if (defaultVal == textoComentario) {
alert("Digite um comentário!");
} else {
document.getElementById("demo").innerHTML = "<p>" + textoComentario + "</p>";
x.value = "";
}
}
</script>
</body>
</html>
['Java'!='JavaScript'] (http://stackoverflow.com/questions/245062/whats-the-difference-between-javascript-and-java) – Pshemo
刪除標誌java –
'document.getElementById(「demo」)。inne rHTML + =' – Zee