在IE中存在.setCapture(); .releaseCapture()函数。 在没有使用jQuery的情况下,Firefox中这些函数的等效功能是什么? (我的客户不想使用它)在Firefox中捕获鼠标
回答
FF/JavaScript中没有这样的功能。捕获函数仅存在于JScript中。
SetCapture和ReleaseCapture是您发现的IE propriatory功能。在Firefox中没有本地的方式来操作内容菜单。可以在http://www.codeplex.com/gimme/Wiki/Recent.aspx找到Gimme。这里有一篇博客文章:http://blog.stchur.com/2007/11/21/setcapture-with-gimme,其中描述了使用它来替换函数的一个场景。
Firefox从版本4开始添加了setCapture和releaseCapture,请参见[Chip Cressman的回答](http://stackoverflow.com/a/13829499/1421194)和[Kaj Dijkstra的回答](https://developer.mozilla.org/samples /domref/mousecapture.html)。 – Sasha 2016-01-21 16:39:55
setCapture()和releaseCapture()是Internet Explorer特定的非标准方法。 Firefox中没有实现。有一个名为Gimme的框架,它提供了一些鼠标捕获功能。 http://www.codeplex.com/gimme/
Firefox从版本4开始添加了setCapture和releaseCapture,请参见[Chip Cressman的回答](http://stackoverflow.com/a/13829499/1421194)和[Kaj Dijkstra的回答](https://developer.mozilla.org/samples /domref/mousecapture.html)。 – Sasha 2016-01-21 16:39:47
使用事件冒泡:将冒泡鼠标事件的事件侦听器添加到高级容器(可能甚至是document
),并使用变量来追踪哪个元素应该是捕获的元素。
没有关于你想要做什么的进一步信息,没有什么可说的。
我想实现一个页面分割条 – Cornel 2009-05-05 07:51:27
如上所述,Firefox不提供此功能,您可以通过监视整个文档上的事件来解决此问题。为了确保没有更好的技巧,我刚刚检查过jQuery UI,看起来他们使用相同的方法。因此,举例来说,如果你想捕捉鼠标移动时,鼠标在jQuery的了,你会怎么做:
$("#someElement").
mousedown(function() { $(document).mousemove(captureMouseMove) }).
mouseup(function() { $(document).unbind("mousemove", captureMouseMove) });
function captureMouseMove(event)
{
// ...
}
Firefox从版本4开始添加了setCapture和releaseCapture,请参见[Chip Cressman的回答](http://stackoverflow.com/a/13829499/1421194)和[Kaj Dijkstra的回答](https: //developer.mozilla.org/samples/domref/mousecapture.html)。 – Sasha 2016-01-21 16:39:12
捕捉随时鼠标不好的行为,我认为这就是为什么不提供setCapture
。
但是,要捕获鼠标进行拖放操作,只需要处理document
对象的鼠标事件(鼠标{上,下,移动}),该对象甚至可以在拖动客户区。
<html>
<head>
<title>Capture test</title>
</head>
<body>
<script type="text/javascript">
document.onmousedown = function() {
state.innerHTML = "Dragging started";
};
document.onmousemove = function (e) {
coord.innerHTML = e.clientX + ',' + e.clientY;
}
document.onmouseup = function (e) {
state.innerHTML = "Dragging stopped";
}
</script>
<p id="state">.</p>
<p id="coord">.</p>
</body>
</html>
我相信element.setCapture()和document.releaseCapture()加入到Firefox作为FF4的: https://developer.mozilla.org/en/DOM/element.setCapture
“mouseDown事件的处理期间调用element.setCapture()方法将所有鼠标事件重定向到此元素,直到释放鼠标按钮或调用document.releaseCapture()。“
https://developer.mozilla.org/en-US/docs/DOM/element.setCapture
setCapture和releaseCapture加入到火狐4(与壁虎2的释放)3月22日,2011年但是,WebKit的(铬/ Safari浏览器)仍缺乏这些功能。
@ JanZich的解决方案效果很好,除非鼠标不在元素中时捕获鼠标事件。这个工作更好地为我:
$("#someElement").mousedown(function() {
$(document).mousemove(captureMouseMove);
$(document).mouseup(captureMouseUp);
});
function captureMouseMove(event) {
console.log("mouse move");
}
function captureMouseUp(event) {
console.log("mouse up");
$(document).unbind("mousemove", captureMouseMove);
$(document).unbind("mouseup", captureMouseUp);
}
使用true
为addEventListener
方法的第三个参数来捕捉。例如:
document.addEventListener("click", function(event){location.hash=event.target}, true)
使用removeEventListener
使用相同的参数来取消:
document.removeEventListener("click", function(event){location.hash=event.target}, true);
参考
- 1. 在Firefox中捕捉鼠标移动
- 2. 捕获鼠标
- 3. 在firefox中获取鼠标坐标41
- 4. WinForms鼠标捕获
- 5. java鼠标捕获
- 6. 捕获(捕获)Java中的窗口中的鼠标光标
- 7. 在UserControl中捕获鼠标点击
- 8. 在python中捕获鼠标单击
- 9. 在C(linux)中捕获鼠标事件
- 10. 捕获鼠标运动
- 11. ffmpeg gdigrab hidpi鼠标捕获
- 12. 捕获鼠标点击php
- 13. 鼠标捕获的差异
- 14. 捕获鼠标事件
- 15. 捕获鼠标移动SVG
- 16. 拖放鼠标捕获
- 17. 鼠标移动捕获
- 18. 鼠标移动捕获(鼠标离开和鼠标输入)
- 19. 捕获鼠标光标图标C++
- 20. 捕获CTRL +在Firefox中
- 21. 在asp.net上捕获鼠标文本框
- 22. 捕获鼠标事件在Python
- 23. Matplotlib捕获错误的鼠标坐标
- 24. 如何获取鼠标捕获元素
- 25. 诱捕鼠标?
- 26. 获取鼠标在Firefox中的坐标18,IE 9,Chrome 24
- 27. 鼠标移动 - 在Firefox
- 28. 如何在屏幕抓取中捕获鼠标光标?
- 29. 如何在Java中捕获鼠标光标?
- 30. 如何在C++ Win32中捕获鼠标移动过标题栏?
你到底要使用这些功能呢?客户很可能想要一些行为,而不是功能,对吗? – 2009-05-04 19:41:34