jQuery: 判断指针是否在某元素内 How do I check if the mouse is over an element

代码:

// required jquery.min.js

$(function()
{ 
        var $current_tab_menu = $('#main_menu');
	$(document).on('mouseover',function(e)
	{ 
		var xx = e.originalEvent.x || e.originalEvent.layerX || 0; 
		var yy = e.originalEvent.y || e.originalEvent.layerY || 0;
		
		if($current_tab_menu && $('.has_submenu:hover').length === 0)
		{
			console.log(($current_tab_menu.is(':hover')?'Y':'N'));
			
			if($current_tab_menu.is(':hover')) $current_tab_menu.show();
			else $current_tab_menu.hide();
		} 
		else console.log('XY:'+(e.pageX + '---' + e.pageY));
	});
})

重点在于,判断的时候用 $(‘.has_submenu:hover’).length === 0 代替  $(‘.has_submenu’).is(‘:hover’)。 原因是当你在 jQuery 1.9中使用.is(“:hover”) 的时候容易参数如下错误提示:

Error: Syntax error, unrecognized expression: unsupported pseudo: hover

本文: jQuery: 判断指针是否在某元素内 How do I check if the mouse is over an element

Loading

Add a Comment

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.