Javascript 如何阻止事件泡沫
在中,页面中有两个div,外部和内部 我将mousemove事件绑定到outer div,当用户在outer div中mousemove时,它将显示鼠标的clientX和clientY 我还将内部div设置为可拖动的,下面是 1) 我不想在拖动内部div时触发外部的mousemove事件 当然,我可以在拖动内部div时设置“outer.onmousemove=null”,但我认为这不是最好的方法,因为外部div上的事件可能被其他人绑定 我尝试使用event.cancalBubble,但它似乎不起作用Javascript 如何阻止事件泡沫,javascript,events,Javascript,Events,在中,页面中有两个div,外部和内部 我将mousemove事件绑定到outer div,当用户在outer div中mousemove时,它将显示鼠标的clientX和clientY 我还将内部div设置为可拖动的,下面是 1) 我不想在拖动内部div时触发外部的mousemove事件 当然,我可以在拖动内部div时设置“outer.onmousemove=null”,但我认为这不是最好的方法,因为外部div上的事件可能被其他人绑定 我尝试使用event.cancalBubble,但它似乎不起
2) 当我拖动内部div时,它将选择firefox中的文本,如何停止它?可以调用两个函数来确保事件气泡停止:
event.stopPropagation();
event.preventDefault();
如果确保同时调用这两个函数,则应防止同时选择父div和文本
编辑:
仔细查看您的代码,我发现了一些问题。第一件事是使用onmousemove事件在“外部”div中注册鼠标坐标。然后使用onmousemove上的文档拖动到“内部”div。这意味着,如果在开始拖动时停止onmousemove事件的传播,它将永远不会冒泡到文档节点,从而导致可拖动div永远不会被拖动,直到您实际将鼠标移到“内部”div区域之外
一种解决方案是在moveable div而不是文档上设置_mouseMove函数,然后像这样停止传播:
/* Remove this */
//document.onmousemove=_mouseMove;
//document.onmouseup=_mouseUp;
/* Add this */
move_ele.onmousemove=_mouseMove;
move_ele.onmouseup=_mouseUp;
e.stopPropagation();
e.preventDefault();
out.onmousemove=function(e){
/* Check to see if the target is the "outer" div */
if(e.target == this){
e=e==null?window.event:e;
note.innerHTML=e.clientX+','+e.clientY;
}
}
这将使它像您提到的那样工作,除非您拖动得太快,以至于光标离开“内部”div区域,然后它将停止拖动,直到光标再次进入div区域
另一个解决方案是在“outer”divs onmousemove事件中处理这一切,以查看鼠标是否确实在该div上移动,而不是像这样在“inner”上移动:
/* Remove this */
//document.onmousemove=_mouseMove;
//document.onmouseup=_mouseUp;
/* Add this */
move_ele.onmousemove=_mouseMove;
move_ele.onmouseup=_mouseUp;
e.stopPropagation();
e.preventDefault();
out.onmousemove=function(e){
/* Check to see if the target is the "outer" div */
if(e.target == this){
e=e==null?window.event:e;
note.innerHTML=e.clientX+','+e.clientY;
}
}
这也会像您提到的那样工作,但它也会在您将光标移动到内部div上时停止坐标的更新,即使您尚未开始拖动。可以调用两个函数来确保事件气泡停止:
event.stopPropagation();
event.preventDefault();
如果确保同时调用这两个函数,则应防止同时选择父div和文本
编辑:
仔细查看您的代码,我发现了一些问题。第一件事是使用onmousemove事件在“外部”div中注册鼠标坐标。然后使用onmousemove上的文档拖动到“内部”div。这意味着,如果在开始拖动时停止onmousemove事件的传播,它将永远不会冒泡到文档节点,从而导致可拖动div永远不会被拖动,直到您实际将鼠标移到“内部”div区域之外
一种解决方案是在moveable div而不是文档上设置_mouseMove函数,然后像这样停止传播:
/* Remove this */
//document.onmousemove=_mouseMove;
//document.onmouseup=_mouseUp;
/* Add this */
move_ele.onmousemove=_mouseMove;
move_ele.onmouseup=_mouseUp;
e.stopPropagation();
e.preventDefault();
out.onmousemove=function(e){
/* Check to see if the target is the "outer" div */
if(e.target == this){
e=e==null?window.event:e;
note.innerHTML=e.clientX+','+e.clientY;
}
}
这将使它像您提到的那样工作,除非您拖动得太快,以至于光标离开“内部”div区域,然后它将停止拖动,直到光标再次进入div区域
另一个解决方案是在“outer”divs onmousemove事件中处理这一切,以查看鼠标是否确实在该div上移动,而不是像这样在“inner”上移动:
/* Remove this */
//document.onmousemove=_mouseMove;
//document.onmouseup=_mouseUp;
/* Add this */
move_ele.onmousemove=_mouseMove;
move_ele.onmouseup=_mouseUp;
e.stopPropagation();
e.preventDefault();
out.onmousemove=function(e){
/* Check to see if the target is the "outer" div */
if(e.target == this){
e=e==null?window.event:e;
note.innerHTML=e.clientX+','+e.clientY;
}
}
这也会像您提到的那样工作,但它也会在您将光标移动到内部div上时停止更新坐标,即使您尚未开始拖动