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 = ‘‘;}
时间: 2024-10-26 11:49:12