简体   繁体   English

如何选择除特定元素之外的所有内容?

[英]How can I select everything except specific element?

Here is my code: 这是我的代码:

 $.fn.right = function() { return $(document).width() - (this.offset().left + this.outerWidth()); } $(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), right = self.right(); $('body').append("<div class='tag_info'>Some explanations about "+tag_name+"</div>"); $(".tag_info").css({top: top + "px", right: right + "px"}).fadeIn(200); }, 525); }).bind('mouseleave', function(){ if(this.iid){ clearTimeout(this.iid) $('.tag_info').remove(); } }); }); 
  body{ padding: 20px; direction: rtl; } 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>long-length-tag</a> <a>tag</a> 

It works as well and that pop-up will be removed when your mouse leaves the tag. 它也可以正常工作,当鼠标离开标签时,弹出窗口将被移除。 Now I want to keep that pop-up when the mouse leaves the tag and goes on that pop-up. 现在,当鼠标离开标签并继续弹出时,我想保留该弹出窗口。 Otherwise it should be removed. 否则应将其删除。 How can I do that? 我怎样才能做到这一点?

You can add a condition to check if the mouse is hovering over the pop-up before removing it: 您可以添加条件以检查鼠标是否在移除弹出窗口之前将其悬停在弹出窗口上:

if ($('.tag_info:hover').length == 0) {....}

And you need to add an event on the pop-up itself to remove it on mouseleave 并且您需要在弹出窗口中添加一个事件以在mouseleave上删除它

See code snippet: 请参阅代码段:

 $.fn.right = function() { return $(document).width() - (this.offset().left + this.outerWidth()); } $(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), right = self.right(); var pop_up = $('<div />', { "class": 'tag_info', text: "Some explanations about " + tag_name, mouseleave: function(e){ $(this).remove(); }}) $('body').append(pop_up); $(".tag_info").css({ top: top + "px", right: right + "px" }).fadeIn(200); }, 525); }).bind('mouseleave', function() { if (this.iid) { clearTimeout(this.iid) if ($('.tag_info:hover').length == 0) { $('.tag_info').remove(); } } }); }); 
 body { padding: 20px; direction: rtl; } 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>long-length-tag</a> <a>tag</a> 

Now I want to keep that pop-up when the mouse leaves the tag and goes on that pop-up. 现在,当鼠标离开标签并继续弹出时,我想保留该弹出窗口。

You could use MouseEvent.relatedTarget to check which element the mouse left to. 您可以使用MouseEvent.relatedTarget来检查鼠标左侧的元素。

If you add an event listener on mouseleave , an event object the is passed into the event callback when the mouse leaves the element. 如果在mouseleave上添加事件侦听器,则当鼠标离开元素时,会将事件对象传递给事件回调。 In that event object is the property relateTarget which is a pointer to the element the mouse left on to. 在该事件中,对象是属性relateTarget ,它是指向鼠标所在元素的指针。 So if the mouse leaves the element on to the tag_info element, you can make the info not be hidden. 因此,如果鼠标将元素留在tag_info元素上,则可以使信息不被隐藏。

You can also wrap the event in a jquery selector to create a jquery object if you prefer like so: 您也可以将事件包装在jquery选择器中,以创建一个jquery对象,如果您喜欢这样:

$(e.relateTarget) // do something with the jquery object

Try hovering over the tag and then pointing your mouse on top of the tag_info 尝试将鼠标悬停在标记上,然后将鼠标指向tag_info顶部

Hopefully that helps! 希望这有帮助!

 $.fn.right = function() { return $(document).width() - (this.offset().left + this.outerWidth()); } $(document).ready(function() { $('a').bind('mouseenter', function() { var self = $(this); var iid = this.iid = setTimeout(function() { var tag_name = self.text(), top = self.position().top + self.outerHeight(true), right = self.right(); $('body').append("<div class='tag_info'>Some explanations about " + tag_name + "</div>"); var tagInfo = $(".tag_info"); tagInfo.css({ top: top + "px", right: right + "px" }).fadeIn(200); tagInfo.bind('mouseleave', function (e) { console.log('mouse left tag info'); if (iid) { clearTimeout(iid) tagInfo.remove(); } }); }, 525); }).bind('mouseleave', function(e) { //this is the event callback and the event object is `e` if (e.relatedTarget && e.relatedTarget.classList.contains('tag_info')) { console.log('mouse left onto the tag_info'); } else { console.log('mouse left onto something else'); if (this.iid) { clearTimeout(this.iid) $('.tag_info').remove(); } } }); }); 
 body { padding: 20px; direction: rtl; } 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>long-length-tag</a> <a>tag</a> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM