2009-05-04 119 views
4

在IE中存在.setCapture(); .releaseCapture()函數。 在沒有使用jQuery的情況下,Firefox中這些函數的等效功能是什麼? (我的客戶不想使用它)在Firefox中捕獲鼠標

+1

你到底要使用這些功能呢?客戶很可能想要一些行爲,而不是功能,對嗎? – 2009-05-04 19:41:34

回答

-3

FF/JavaScript中沒有這樣的功能。捕獲函數僅存在於JScript中。

+0

我知道這一點。它也必須是FF的解決方案 – Cornel 2009-05-04 13:18:57

+0

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:36:35

0

SetCapture和ReleaseCapture是您發現的IE propriatory功能。在Firefox中沒有本地的方式來操作內容菜單。可以在http://www.codeplex.com/gimme/Wiki/Recent.aspx找到Gimme。這裏有一篇博客文章:http://blog.stchur.com/2007/11/21/setcapture-with-gimme,其中描述了使用它來替換函數的一個場景。

+0

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

0

setCapture()和releaseCapture()是Internet Explorer特定的非標準方法。 Firefox中沒有實現。有一個名爲Gimme的框架,它提供了一些鼠標捕獲功能。 http://www.codeplex.com/gimme/

+0

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

1

使用事件冒泡:將冒泡鼠標事件的事件偵聽器添加到高級容器(可能甚至是document),並使用變量來追蹤哪個元素應該是捕獲的元素。

沒有關於你想要做什麼的進一步信息,沒有什麼可說的。

+0

我想實現一個頁面分割條 – Cornel 2009-05-05 07:51:27

12

如上所述,Firefox不提供此功能,您可以通過監視整個文檔上的事件來解決此問題。爲了確保沒有更好的技巧,我剛剛檢查過jQuery UI,看起來他們使用相同的方法。因此,舉例來說,如果你想捕捉鼠標移動時,鼠標在jQuery的了,你會怎麼做:

$("#someElement"). 
    mousedown(function() { $(document).mousemove(captureMouseMove) }). 
    mouseup(function() { $(document).unbind("mousemove", captureMouseMove) }); 

function captureMouseMove(event) 
{ 
    // ... 
} 
+0

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

2

捕捉隨時鼠標不好的行爲,我認爲這就是爲什麼不提供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> 
2

我相信element.setCapture()和document.releaseCapture()加入到Firefox作爲FF4的: https://developer.mozilla.org/en/DOM/element.setCapture

「mouseDown事件的處理期間調用element.setCapture()方法將所有鼠標事件重定向到此元素,直到釋放鼠標按鈕或調用document.releaseCapture()。「

2

@ 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); 
}