大家好,我遇到了一个与嵌套元素的悬停处理有关的问题。当鼠标进入子 child
div 时,祖先元素也处于 hover
状态。有没有办法在鼠标进入子元素时触发祖先元素的 hoverout
事件?
以下是我迄今为止所做的,请审核。
<style>
div
{
border:1px solid red;
margin:10px;
padding:10px;
}
</style>
<script>
$(function() {
$('div').each(function(){
var current = this;
$(this).hover(function(event){
event.stopPropagation();// doesn't work.
console.log('Capture for hover in ' + current.tagName + '#'+ current.id +
' target is ' + event.target.id); },
function(event){
event.stopPropagation();
console.log('Capture for hover out' + current.tagName + '#'+ current.id +
' target is ' + event.target.id); });
});
});
</script>
<body id="greatgrandpa">
<div id="grandpa">
<div id="parent">
<div id="child"/>
</div>
</div>
</body>