[英]jquery add new div onclick
我有一個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代碼,但沒有用。
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添加新的div(作為Twitter回復)? 在我寫一些代碼之前。 它正在工作,但是有一個問題:它僅在一個div上工作。
嘗試
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不太熟悉,但是您可以通過javascript添加新的div
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 newdiv=document.creatElement(NewContent);
var toattach= document.getElementById('comment-body');
toattach.appendchild(newdiv) ;
}
else {
$('.comment-body').eq(index2).next().toggle();
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.