2017-03-07 65 views
0

我有2个重叠的div,并且需要执行相同的mousemove事件。我不能使用具有冒泡效果的嵌套div。 我该怎么做?我不能使用具有冒泡效果的嵌套div。2个重叠divs上的mousemove事件

$(".one").mousemove(function(){ 
 
    console.log("something") 
 
}) 
 

 
$(".two").mousemove(function(){ 
 
    console.log("something else") 
 
})
.one{ 
 
    background-color:red; 
 
    position:absolute; 
 
    left:0; 
 
    top:0; 
 
    width:100px; 
 
    height:100px; 
 
    
 
    
 
} 
 
.two{ 
 
    color:blue; 
 
    position:absolute; 
 
    left:0; 
 
    top:0; 
 
    width:100px; 
 
    height:100px; 
 
    
 
    
 
}
<!DOCTYPE html> 
 
<html> 
 
<head> 
 
    <meta charset="utf-8"> 
 
    <meta name="viewport" content="width=device-width"> 
 
    <title>JS Bin</title> 
 
</head> 
 
<body> 
 
<script src="https://code.jquery.com/jquery-3.1.0.js"></script> 
 

 
    
 
    
 
    <div class="one"> 
 
     Content one 
 
    </div> 
 
    <div class="two"> 
 
     Content two 
 
    </div> 
 

 
</body> 
 
</html>

谢谢。

+0

你可以触发其他分区鼠标移动事件 –

回答

3

$(".one").mousemove(function(){ 
 
    console.log("something"); 
 
}); 
 

 
$(".two").mousemove(function(){ 
 
    console.log("something else"); 
 
    $(".one").mousemove(); 
 
});
.one{ 
 
    background-color:red; 
 
    position:absolute; 
 
    left:0; 
 
    top:0; 
 
    width:100px; 
 
    height:100px; 
 
    
 
    
 
} 
 
.two{ 
 
    color:blue; 
 
    position:absolute; 
 
    left:0; 
 
    top:0; 
 
    width:100px; 
 
    height:100px; 
 
    opacity: .95; 
 
    
 
}
<!DOCTYPE html> 
 
<html> 
 
<head> 
 
    <meta charset="utf-8"> 
 
    <meta name="viewport" content="width=device-width"> 
 
    <title>JS Bin</title> 
 
</head> 
 
<body> 
 
<script src="https://code.jquery.com/jquery-3.1.0.js"></script> 
 

 
    
 
    
 
    <div class="one"> 
 
     Content one 
 
    </div> 
 
    <div class="two"> 
 
     Content two 
 
    </div> 
 

 
</body> 
 
</html>

+0

非常感谢你 – SEV

+0

欢迎您! – Banzay