简体   繁体   English

使用 jQuery 将鼠标悬停在两个元素上

[英]Hover on two elements using jQuery

I have two <div> elements attached to each other, I mean there is no space between them.我有两个<div>元素相互连接,我的意思是它们之间没有空格。

<div id="box1">1</div>
<div id="box2">2</div>

And I have this jQuery code:我有这个 jQuery 代码:

$('#box1 , #box2').hover(function() {
  console.log("Hovered")
}, function() {
  console.log("Not")
});

My problem is when I move the mouse between box1 and box2, I still get on console log "Not".我的问题是当我在 box1 和 box2 之间移动鼠标时,我仍然看到控制台日志“Not”。 I want those divs to be considered as one element so when I move the mouse between them I don't get on console log "Not".我希望将这些 div 视为一个元素,因此当我在它们之间移动鼠标时,我不会进入控制台日志“Not”。

Thanks in advance!提前致谢!

I want those divs to be considered as one element我希望这些 div 被视为一个元素

Well, quite simply, they aren't.嗯,很简单,他们不是。 And they can't be.他们不可能。 That's not how HTML and CSS works.这不是 HTML 和 CSS 的工作方式。

The hover event is triggered one for each individual element bound to the event handler.为绑定到事件处理程序的每个单独元素触发悬停事件。 And every time you leave one of those elements it will print the "not" output as per your instructions.每次您离开这些元素之一时,它都会根据您的说明打印“not”输出。

There is no "fix" for this in the exact way you described, but there are alternative approaches.没有按照您描述的确切方式对此进行“修复”,但有其他方法。 An obvious solution is to wrap them both in an outer div and bind the hover event to that instead.一个明显的解决方案是将它们都包装在一个外部 div 中,并将悬停事件绑定到它。 Then the whole area will be considered as one element (because it literally is).然后整个区域被视为一个元素(因为它确实是)。 Demo:演示:

 $('#boxcontainer').hover(function() { console.log("Hovered") }, function() { console.log("Not") });
 #boxcontainer { border: solid 1px black; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="boxcontainer"> <div id="box1">1</div> <div id="box2">2</div> </div>

friend check the code below.朋友检查下面的代码。 I think it will work for you.我认为它对你有用。 As you have dai you have an absolute position div you must need a parent div and the parent div position must be relative.因为你有 dai 你有一个绝对位置 div 你必须需要一个父 div 并且父 div 位置必须是相对的。 For doing that you have to add just a simple CSS code position: relative;为此,您只需添加一个简单的 CSS 代码position: relative; . . You also need to do some changes to your jquery code.您还需要对 jquery 代码进行一些更改。 You can just hover on the parent div and it will do your work.您只需将鼠标悬停在父 div 上即可完成您的工作。 Hope this code will help you.希望这段代码能帮到你。

 //Box 1 Demo $('#boxParrent1').hover(function() { console.log("Hovered") }, function() { console.log("Not") }); //Box 2 Demo $('#boxParrent2').hover(function() { console.log("Hovered") }, function() { console.log("Not") });
 /*Main Code that are needed*/ #boxParrent1, #boxParrent2 { position: relative; } /*Codes Just used to give you a demo*/ #boxParrent1, #boxParrent2{ display: flex; margin-bottom: 50px; border: 1px solid #000; } #boxParrent1{ width: 200px; } #boxParrent2{ width: 210px; } #box1, #box2, #box3, #box4{ background: tomato; width: 100px; height: 100px; display: grid; justify-content: center; align-items: center; font-family: Arial; font-size: 50px; color: #fff; cursor: pointer; } #box2, #box4{ position:absolute; top: 0; left:100px; background: #02dce6; } #box4{ left:110px; background: #02dce6; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="boxParrent1"> <div id="box1">1</div> <div id="box2">2</div> </div> <div id="boxParrent2"> <div id="box3">3</div> <div id="box4">4</div> </div>

Try to place your 2 div 's in one super div尝试将您的 2 div放在一个超级div

<div id="super">
   <div id="box1">1</div>
   <div id="box2">2</div>
</div>

$('#super').hover(function() {
   console.log("Hovered")
 }, function() {
   console.log("Not")
 });

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

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