2017-07-09 9 views
-1

Here私のコードです:.remove()は要素を削除しないのはなぜですか?

$(document).ready(function(){ 
    $('a').bind('mouseenter', function() { 
    var self = $(this); 
    this.iid = setTimeout(function() { 
     var tag_name = self.text(), 
      top  = self.position().top + self.outerHeight(true), 
      left  = self.position().left; 
     $('body').append("<div class='tag_info'>Some explanations about "+tag_name+"</div>"); 
     $(".tag_info").css({top: top + "px", left: left + "px"}).fadeIn(200); 
    }, 525); 
    }).bind('mouseleave', function(){ 
    if(this.iid){ 
     clearTimeout(this.iid) 
     remove($('.tag_info')); 
    } 
    }); 
}); 

あなたは、私はブラックボックスがまだ存在していることを、あなたのマウスがタグを離れるとき、提供してきましたフィドルで見たよう。どうして?どうすれば削除できますか?

+2

てみ '$のようにそれを使用する必要がdocumentation

remove($('.tag_info')); // Not Correct 

ごとにそれを使用してください( 'tag_info')。削除()あなたは'であることをしなかった理由を ' – ThisGuyHasTwoThumbs

+0

:ホバー? –

答えて

6

代わりに以下のコードを使用してください。

$('.tag_info').remove(); 
2
remove($('.tag_info')); 

jQueryのセレクタに

0
$('.tag_info').remove(); 

なければならない最初に定義します。

$('.tag_info').remove(); 
1

$(document).ready(function(){ 
 
    $('a').bind('mouseenter', function() { 
 
    var self = $(this); 
 
    this.iid = setTimeout(function() { 
 
     var tag_name = self.text(), 
 
      top  = self.position().top + self.outerHeight(true), 
 
      left  = self.position().left; 
 
     $('body').append("<div class='tag_info'>Some explanations about "+tag_name+"</div>"); 
 
     $(".tag_info").css({top: top + "px", left: left + "px"}).fadeIn(200); 
 
    }, 525); 
 
    }).bind('mouseleave', function(){ 
 
    $('.tag_info').remove(); 
 
    }); 
 
});
body{ 
 
     padding: 20px; 
 
    } 
 

 
    a { 
 
     color: #3e6d8e !important; 
 
     background-color: #E1ECF4; 
 
     padding: 2px 5px; 
 
    } 
 
    .tag_info{ 
 
     position: absolute; 
 
     width: 130px; 
 
     height: 100px; 
 
     display:none; 
 
     background-color: black; 
 
     color: white; 
 
     padding: 10px; 
 
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
    <a>tag1</a> 
 
    <a>tag2</a>

check the code `https://jsfiddle.net/uz6y3L2y/3/` may help you. 
0

あなたはそれがクラス.tag_infoを持っている要素を削除しない理由です、間違った方法でjqueryのfunciton remove()を使用しています。あなたはこの

$('.tag_info').remove(); 
関連する問題