jQuery:如何获取光标下的元素?

Nul*_*uli 7 jquery

我正在做一个拖放操作,当我将一个元素放在我的方式上时,我想要一个元素向下移动.

Jer*_*oyd 9

一个简单的黑客就是这样说:

var hoverElem = null;
$('*').hover(function() {hoverElem = this});
Run Code Online (Sandbox Code Playgroud)

然后当你需要调用什么函数来获取值时,只需使用hoverElem

编辑:这是一个资源密集度较低的电话:

$('*').live('mouseenter', function() { hoverEleme = this; });
Run Code Online (Sandbox Code Playgroud)

编辑2:由于.live()被弃用,你应该使用.on()

$("*").on("mouseenter", function() { hoverElem = this; });
Run Code Online (Sandbox Code Playgroud)

  • 而不是将鼠标处理程序附加到每个元素(也在`mouseleave`上触发你应该使用`$('*').live('mouseenter',function(){hoverEleme = this;});`,它是*很多*便宜,特别是你得到的元素越多. (10认同)

ant*_*upe 8

$('*').on('mouseenter', function() { hoverEleme = this; });
Run Code Online (Sandbox Code Playgroud)

这比将鼠标处理程序附加到每个元素(也在mouseleave上触发)要便宜得多,尤其是你得到的元素越多.