2015-07-01 70 views
0

我有這段代碼,它不能按我想要的方式工作。我希望它在每次單擊按鈕時在另一個下創建一個新的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> 
+2

['Java'!='JavaScript'] (http://stackoverflow.com/questions/245062/whats-the-difference-between-javascript-and-java) – Pshemo

+0

刪除標誌java –

+0

'document.getElementById(「demo」)。inne rHTML + =' – Zee

回答

0

我認爲你正在尋找這樣的:

WORKING:DEMO

HTML

NO CHANGE 

CSS

NO CHANGE 

個JS/JQuery的

function myFunction() { 
    var x = document.getElementById("myText"); 
    var defaultVal = x.defaultValue; 
    var textoComentario = x.value; 
    alert(textoComentario); 

    if (defaultVal == textoComentario) { 
     alert("Digite um comentário!"); 
    } else { 
      $("#demo").css("display","block"); 
      $("#demo").append("<p>" + textoComentario + "</p>"); 
     x.value = ""; 

    } 
} 
0

更換你

document.getElementById("demo").innerHTML = "<p>" + textoComentario + "</p>"; 

$('#demo').append("<p>" + textoComentario + "</p>");

它將工作.......