jQuery的的onclick添加新的div [英] jquery add new div onclick
本文介绍了jQuery的的onclick添加新的div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有DIV名单
<li class="comment">
<div class="comment-body" id="comment-body">
<div class="comment-author vcard">
<div class="lightbox-photo">
<a class="image-overlay" href='<%# "Foto/profil/foto_buyuk/" + Eval("Yorum_Profil_Foto_Buyuk") %>' data-rel="prettyPhoto" title='<%# Eval("Yorum_UserName")%>'><img src='<%# "Foto/profil/foto_kucuk/" + Eval("Yorum_Profil_Foto_Kucuk") %>' alt='<%# Eval("Yorum_UserName")%>' class="avatar" />
</a>
</div>
<cite class="fn"><asp:HyperLink ID="linkProfil" runat="server" Text='<%# Eval("Yorum_UserName")%>' NavigateUrl='<%# "~/profil.aspx?user_id="+Eval("User_ID") %>'></asp:HyperLink></cite>
<cite class="fn-time"></cite>
</div>
<p><%# Eval("Yorum_Text")%></p>
</div>
</div>
</div>
</li>
我想补充的DIV点击一个新的div。我写了jQuery的codeS,但它不工作。
I want to add a new div on div click. I wrote jquery codes but it doesnt work.
function addcommentdiv () {
var NewContent = '<div class=""><input name="name" type="text" id="name" size="20" value="" style="height:20px; margin-top:10px; width:480px;margin-left:90px; font-size:14px;" /></div>'
$('.comment-body').click(function () {
var index2 = $('.comment-body').index(this);
if (NewContent != '') {
$('.comment-body').eq(index2).after(NewContent);
NewContent = '';
}
else {
$('.comment-body').eq(index2).next().toggle();
}
});
};
为什么它不工作,或者我如何添加一个新的div来点击以下(如Twitter回复)DIV?之前我写了一些code。这是工作,但有一个问题:它的工作只有一个分区
Why it doesn't work or how can I add a new div to clicked div below (as twitter reply)? Before I wrote some code. It was working but there was a problem: it was working for one div only.
推荐答案
尝试
function addcommentdiv () {
var NewContent = '<div class="reply"><input name="name" type="text" id="name" size="20" value="" style="height:20px; margin-top:10px; width:480px;margin-left:90px; font-size:14px;" /></div>'
$('.comment-body').click(function () {
var $this = $(this), $reply = $this.next('.reply');
if ($reply.length) {
$reply.toggle();
} else {
$(NewContent).insertAfter($this);
}
});
};
演示:小提琴
这篇关于jQuery的的onclick添加新的div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文