2017-04-12 2 views
1

요소 위에 마우스를 가져 가면 이미 존재하는 div를 표시하려고합니다. 요소에 div와 일치하는 ID가 있습니다.복제 된 div (jQuery) 제거

복제 및 표시 부분은 호버에서 작동하지만 이미 복제 된 요소를 제거했습니다. 나는 다른 대답에서 언급 된 벽장을 보았지만 아마 틀리게 사용했다. 그래서 단순히 당신이 현재 요소 this 제거 내부 '#reply_' + id을 찾습니다

$(this).find('#reply_' + id).remove(); 

:

$('.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>

답변

2

당신처럼, 대신 closest()이 경우 기능 만 find() 필요하지 않습니다 그것.

희망이 도움이됩니다.

$('.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>