[英]Removal of a cloned div (jQuery)
悬停在元素上方时,我想显示一个已经存在的div。 元素的ID与div相匹配。
克隆和显示部分可以在悬停时工作,但我坚持删除已克隆的元素。 我在另一个答案中看到壁橱,但我可能用错了。
$('.referer').hover(function() { var id = $(this).attr('id') $('#reply_' + id).clone().appendTo(this); }, function() { var id = $(this).attr('id') $('#reply_' + id).closest(this).remove(); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="reply_1"> First Post </div> <div id="reply_2"> Second Post </div> <div id="reply_3"> Third Post </div> <!--The id is the id of the quoted post--> <p> <span class="referer" id="1">Quoted Link (First Post)</span>
在这种情况下,您不需要closest()
函数,而是需要使用find()
,例如:
$(this).find('#reply_' + id).remove();
所以,简单地你寻找'#reply_' + id
当前元素中this
并删除它。
希望这可以帮助。
$('.referer').hover(function() { var id = $(this).attr('id') $('#reply_' + id).clone().appendTo(this); }, function() { var id = $(this).attr('id') $(this).find('#reply_' + id).remove(); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="reply_1">First Post</div> <div id="reply_2">Second Post</div> <div id="reply_3">Third Post</div> <!--The id is the id of the quoted post--> <p> <span class="referer" id="1">Quoted Link (First Post)</span> <br> <span class="referer" id="2">Quoted Link (Second Post)</span> <br> <span class="referer" id="3">Quoted Link (Third Post)</span>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.