- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我有一个 <ul>
在初始页面加载后用 javascript 填充。我目前正在使用 .bind
与 mouseover
和 mouseout
.
该项目刚刚更新到 jQuery 1.7,所以我可以选择使用 .on
, 但我似乎无法让它与 hover
一起使用.是否可以使用 .on
与 hover
?
编辑:我绑定(bind)的元素在文档加载后使用 javascript 加载。这就是我使用 on
的原因而不仅仅是 hover
.
最佳答案
(如果您需要将 .on()
与填充有 JavaScript 的元素一起使用,请查看此答案中的最后编辑)
将此用于未使用 JavaScript 填充的元素:
$(".selector").on("mouseover", function () {
//stuff to do on mouseover
});
.hover()
有自己的处理程序:http://api.jquery.com/hover/
如果你想做多件事,将它们链接到 .on()
处理程序中,如下所示:
$(".selector").on({
mouseenter: function () {
//stuff to do on mouse enter
},
mouseleave: function () {
//stuff to do on mouse leave
}
});
根据下面提供的答案,您可以将 hover
与 .on()
一起使用,但是:
Although strongly discouraged for new code, you may see the pseudo-event-name "hover" used as a shorthand for the string "mouseenter mouseleave". It attaches a single event handler for those two events, and the handler must examine event.type to determine whether the event is mouseenter or mouseleave. Do not confuse the "hover" pseudo-event-name with the .hover() method, which accepts one or two functions.
此外,使用它没有性能优势,而且比仅使用 mouseenter
或 mouseleave
更笨重。我提供的答案需要更少的代码,是实现类似目标的正确方法。
编辑
自从回答这个问题以来已经有一段时间了,它似乎获得了一些关注。上面的代码仍然有效,但我确实想在我原来的答案中添加一些内容。
虽然我更喜欢将 mouseenter
和 mouseleave
(帮助我理解代码中发生的事情)与 .on()
一起使用,但它只是与使用 hover()
$(".selector").hover(function () {
//stuff to do on mouse enter
},
function () {
//stuff to do on mouse leave
});
由于最初的问题确实询问了如何将 on()
与 hover()
一起正确使用,所以我想我会更正 on() 的用法
并且当时没有发现有必要添加 hover()
代码。
2012 年 12 月 11 日编辑
下面提供的一些新答案详细说明了如果所讨论的 div
是使用 JavaScript 填充的,.on()
应该如何工作。例如,假设您使用 jQuery 的 .load()
事件填充 div
,如下所示:
(function ($) {
//append div to document body
$('<div class="selector">Test</div>').appendTo(document.body);
}(jQuery));
上面的 .on()
代码是站不住脚的。相反,您应该稍微修改您的代码,如下所示:
$(document).on({
mouseenter: function () {
//stuff to do on mouse enter
},
mouseleave: function () {
//stuff to do on mouse leave
}
}, ".selector"); //pass the element as an argument to .on
此代码适用于在 .load()
事件发生后用 JavaScript 填充的元素。只需将您的参数更改为适当的选择器即可。
关于jquery - 是否可以使用 jQuery .on 和悬停?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9827095/
我正在学习和使用 jQuery,并且想要显示某些元素的删除图标。 我有一个外部主 div,其中包含许多元素的包装器。在元素包装器内部,我想当用户将鼠标悬停在元素包装器上时显示删除图标,并在用户移出元素
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我从 Qt 开始,每当有人将鼠标悬停在 QPushButton 上时,我想将 QPushButton 设置为不同的图标。到目前为止,这是我的代码: #include QPushButton *but
我从 Qt 开始,我想在有人将鼠标悬停在 QPushButton 上时将其设置为不同的图标。到目前为止,这是我的代码: #include QPushButton *button = new QPus
我有以下代码... jQuery(function($) { var timer; $('.action-viewer').hide(); $('.task').on('mouseover
我有一张照片,当用户将鼠标悬停在它上面时,我会在照片顶部显示(使用绝对定位)2 个箭头,一个用于进入上一张照片,一个用于进入下一张照片。这是到目前为止的代码... $('a.large_product
$('li > a').hover( function(){ $(this).animate({ backgroundColor: '#2a639a', color: '#fff'
我有一个悬停事件附加到几个链接,当您点击它时,会出现一个框。 有没有一种方法可以让悬停事件仅在鼠标悬停在链接上超过 500 毫秒时触发?因此,目前只要鼠标移到链接上,就会出现该框,但我希望只有当鼠标悬
我正在创建包含各种样式控件的演示应用程序。它允许我快速预览更改。 我的问题是下面的代码不起作用: Pressed button 它表示 IsPressed 的 setter 受到保护。我明白了,但我需
我正在尝试使用 js 并创建带有一些“信息文本”的“描述框” HTML google JS function info() {} 我不知道是哪个代码用一些文本创建了所谓的“描
我有一个 Accordion 风格的菜单,在悬停状态下工作正常,但我想改为点击... $(document).ready(function(){ $('#nav > li').hover(
我正在使用 Chartjs v.1.0.2 并尝试将点设置为仅在悬停在当前点上时出现。同样的问题是当我使用悬停或鼠标悬停时。可以使用 getPointsAtEvent(e); 找到当前点,但它仅在我将
我使用foreach 语句访问IEnumerable 的所有元素并将其呈现在详细信息页面中。我想让每个元素的悬停属性只影响一个元素。所以我使用 jQuery 单独影响每个元素 但是当我运行代码并将鼠标
首先提前感谢您的帮助。 案例:我在一行中有多个 div。这些 div 位于一个框中,我可以在此框中水平滚动以查看其他 div。我制作了两个按钮(L 表示左侧,R 表示右侧),以便在悬停或单击这些按钮时
我正在创建一个应用外观页面。 我希望在鼠标悬停时打开底部菜单上的其中一个按钮。 这感觉像是一项简单的任务,但我所做的一切似乎都不起作用。我错过了什么,我做了很多研究,但似乎找不到解决方案。 我尝试使用
我遇到的问题是,当您将鼠标悬停在按钮上时,只有按钮的某些部分会触发悬停/可点击状态,而不是所有实际 block 。有什么想法吗? 这是使用它的站点:http://www.revival.tv/turn
我有一位客户想要在他们的网站上实现特定功能,但我有点困惑如何做到这一点。 基本上,如果你查看他们的 existing site您可以看到 4 个圆形按钮。 他们想要的是,当有人将鼠标悬停(或单击,如果
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 关闭 9 年前。 关于您编写的代码问题的问题必须在问题本身中描述具体问题 — 并且包括有效代码 以
我目前正在编写我的第一个响应式网站(总的来说我是一个初学者)并且我遇到了很多障碍。其中之一是我的菜单有问题。我会尽量具体。我有一个用于桌面的水平菜单和一个用于平板电脑/手机的垂直菜单。我不知道如何解决
我正在为网站创建菜单。我正试图在链接上实现悬停 和点击 效果。我希望列表元素 a 上的悬停事件触发彩色动画并添加“事件”类。如果触发了点击和悬停事件,我想为该元素分配相同的类。问题是,一旦元素未悬停并
我是一名优秀的程序员,十分优秀!