所以我正在尝试制作一个简单的评论页面,但我似乎无法让它工作:/代码有问题吗?我尝试只使用 javascript,因为我还没有学过 JQuery
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>
最佳答案
document.getElementById('hello').appendChild=parag;
您没有正确调用 appendChild 函数。您正在分配。你应该做的
document.getElementById('hello').appendChild(parag);
注意:在下面的代码中,由于安全问题,我删除了本地存储。
--
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>
关于javascript - 无法将创建的段落元素放入 div 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45520399/