Jquery .hover() 仅适用于 mouseenter
$(".info-icon").hover(function() {
var a = this.id;
var id = a.substring(10);
var $id = "infobox-"+id;
$('#'+$id).addClass("hover");
},
function() {
$('#'+$id).removeClass("hover");
});
有人看到这个错误吗?
谢谢斯文。
$(".info-icon").hover(function() {
var a = this.id;
var id = a.substring(10);
var $id = "infobox-"+id;
$('#'+$id).addClass("hover");
},
function() {
$('#'+$id).removeClass("hover");
});
Anyone seeing the bug?
Thx Sven.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
看来您正在尝试根据操作获取的元素的
id
向不同元素添加“悬停”类(然后稍后将其删除)盘旋。如果是这样,我会简单地这样做:这可以避免重复逻辑(重复=以后搞砸的机会)。
它没有按照您编写的方式工作的原因是,如 SLaks指出,您的
$id
变量仅作为第一个函数中的局部变量存在;您的第二个函数无权访问它。It would appear that you're trying to add a "hover" class to a different element (and then remove it later) based on manipulating the
id
of the element that gets hovered. If so, I would simply do this:This avoids duplicating the logic (duplication = opportunity for messing it up later).
The reason it didn't work the way you wrote it is, as SLaks points out, that your
$id
variable only exists as a local within your first function; your second function doesn't have access to it.您的
$id
变量是局部变量,仅存在于第一个回调中。相反,您应该将 ID 存储在
$.data
中。Your
$id
variable is a local variable and only exists in the first callback.Instead, you should store the id in
$.data
.将
var $id = "";
放在函数外部,使其成为全局函数,然后在第一个回调中删除$idvar
code>,就像这样$id = "infobox-"+id;
,然后将值存储在全局变量$id
中。现在您可以在第二个回调中使用它。希望这有帮助。
Put
var $id = "";
outside the function, to make it global, and then in the first callback remove thevar
in front of$id
, like so$id = "infobox-"+id;
, this will then store the value in the global variable,$id
.Now you can use it in the second callback. Hope this helps.