我想创建评论系统,输入的评论将被添加到特定的 div。下面是我的代码。
<ul class="comments">
<li>
<a class="commenter_name" href="/">Dushyanth Lion</a>
comment by user
</li>
<li>
<a class="commenter_name" href="/">Saikat Bhattacharjee</a>
comment by user
</li>
</ul>
<div class="comment_entry">
<form method="post" action="#">
<input type="text" placeholder="Leave comment" />
<input type="submit" style="display:none;" onclick="" />
</form>
</div>
你可以弄清楚我在这里做什么。请告诉我如何在提交评论后动态添加“li”?
最佳答案
试试这个(见演示:http://jsfiddle.net/7rkX4/):
var user_name = 'Danil';
$('.comment_entry form').submit(function (e) {
e.preventDefault();
var comment = $('input', this).val();
$('.comments').append('<li><a class="commenter_name" href="/">' + user_name + '</a>' + comment + '</li>');
});
关于javascript - 如何使用 jquery 动态添加特定 div 中的元素。我要添加评论,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12051472/