html部分:

<body>
<h1>添加评论</h1>
<ul id="comments">
<li>你好</li>
<li>hello</li>
</ul>
<textarea id="textarea" rows="8" cols="40"></textarea>
<br>
<button id="submit">提交评论</button>
<script src="./js/添加评论.js"></script>
</body>

JS部分:
var button = document.getElementById('submit');
var textarea = document.getElementById('textarea');
var ul = document.getElementById('comments');

button.onclick = function() {
var commentText = textarea.value;
appendComment(commentText);
clearTextarea();
};

function appendComment(text) {
var newComment = document.createElement('li');
newComment.innerHTML = text;
ul.appendChild(newComment);
}

function clearTextarea() {
textarea.value = '';
}
posted on 2016-06-22 20:42  执着于心  阅读(228)  评论(0)    收藏  举报