2017-08-05 32 views
1

所以我试图做一个简单的评论页面,但我似乎无法得到它的工作:/有没有错的代码?我试图使用只有JavaScript,因为我还没有学会的JQuery无法将创建的段落元素放入div中?

function action(){ 

    var input = document.getElementById('header').value; 
    localStorage.setItem('comment',input); 
    document.getElementById('header').value=" "; 
    var getInput = localStorage.getItem('comment'); 
    var date = Date(); 
    var parag = document.createElement('P') 
    parag.innerText=getInput; 
    document.getElementById('hello').appendChild=parag; 

} 
<textarea id='header' type='text' rows='6' cols='100' name='server'> 
</textarea> 
<input onclick="action();" type="button" value='Comment'> 
<span id='hello'></span> 
+1

也许很明显,但...你* *认识到,JavaScript代码需要包裹在'

0

只要改变你追加的孩子:

document.getElementById('hello').appendChild(parag); 

<!DOCTYPE html> 
 
<html> 
 

 
    <head> 
 
    <link rel="stylesheet" href="style.css"> 
 
    <script src="script.js"></script> 
 
    </head> 
 

 
<script type="text/javascript" charset="utf-8"> 
 
    function action(){ 
 
    var input = document.getElementById('header').value; 
 
    localStorage.setItem('comment',input); 
 
    document.getElementById('header').value=" "; 
 
    var getInput = localStorage.getItem('comment'); 
 
    var parag = document.createElement('P') 
 
    parag.innerText=getInput; 
 
    document.getElementById('hello').appendChild(parag); 
 

 
} 
 

 
</script> 
 
    <body> 
 
<textarea id='header' type='text' rows='6' cols='100' name='server'> 
 
</textarea> 
 
<input onclick="action();" type="button" value='Comment'> 
 
<div id='hello'></div> 
 
</body> 
 

 
</html>

0

Node.appendChild的语法错误。

您可以在DOM Living Standard中看到如何使用它。

appendChild(node)方法在被调用时必须将附加节点的结果返回给上下文对象。

您还应该考虑保留对标题的引用,而不是查询DOM两次。