2017-07-09 78 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

嘗試'$( 'tag_info')。remove()方法' – ThisGuyHasTwoThumbs

+0

你爲什麼不這樣做與' :hover'? –

回答

6

改爲使用下面的代碼。

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

應該是

$('.tag_info').remove(); 
0
在jQuery選擇

限定第一。

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

你在錯誤的方式使用jQuery功能可按remove(),這就是爲什麼它不消除它有.tag_info類的元素。請把它作爲每documentation

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

你需要使用這樣的

$('.tag_info').remove(); 
相關問題