应该在操作后生成的HTML:
<div class="comments_action_207">
<div class="list_item_comments list_item_comments_207">
<div class="comment_separator">text goes here</div>
</div>
</div>操作前的HTML:
<div class="comments_action_207"></div>
<div class="list_item_comments list_item_comments_207"><div class="comment_separator">text goes here</div></div>允许我执行上述操作的JavaScript是:
$(function() {
$('.comments_action_207').click(function() {
var num = this.className.split('_').pop();
$('</div>',{'class':'list_item_comments list_item_comments_' + num})
.append('<div class="comment_separator">text goes here</div>')
.appendTo(this);
});
});我测试了上面的JavaScript,它运行得很好。但我不明白的是,为什么我不必传递一个打开的div标记,如下所示。如果我传递一个开头的div标记代码不像我想要的那样工作。
$('<div></div>',{'class':'list_item_comments list_item_comments_' + num}).如果你能一行行地解释一下,我就更容易理解了。
发布于 2012-09-16 18:59:34
您的<div>没有正确关闭:
$('<div><div/>'
^jQuery接受<div />、<div>和<div></div>,但不接受其他任何东西(即没有HTML属性):http://api.jquery.com/jQuery/#jQuery2
发布于 2012-09-16 19:17:37
请试一下这个...It就行了
$('<div></div>',{'class':'list_item_comments list_item_comments_' + num})https://stackoverflow.com/questions/12449728
复制相似问题