檢測遊標類型
回答
我想你可以閱讀,就像你可以設置它的cursor
CSS屬性,但你必須從一個特定的元素做到這一點因爲AFAIK無法從窗口或文檔對象中讀取光標類型。按照這個邏輯,爲了獲得當前的遊標類型,你必須找到鼠標所在的當前元素,並讀取它的cs。然而,你必須經常檢查光標是否改變,這是緩慢且容易出錯的(通常,你應該總是嘗試將你的代碼放入事件處理程序來對事物作出反應,而不是經常檢查它發生了,並把你的代碼在這個功能更邏輯,高效,健壯,清晰。)
但檢測遊標類型的想法仍然迷住了我,如果有人知道我怎麼會喜歡聽到它。 :d
作爲一個替代的解決方案,而不是讀遊標類型,你爲什麼不只是設置,當它進入,將改變它的元素的事件處理程序?這將會減少很多錯誤,並且可能更直接,因爲我不認爲你對光標太在意,但是如果鼠標輸入了特定的元素。
$("#textbox").mouseover(function() {
//I know the cursor has changed because it is now in a textbox
});
你能做到這一點,但它不是漂亮,而且很可能會很慢取決於你有多少個元素有你的頁面上。
$('*').mouseenter(function(){
var currentCursor = $(this).css('cursor') ;
//do what you want here, i.e.
console.log(currentCursor);
});
我有一個很好的jQuery的擴展非常適合在此依據這種類型的事情:
要使用它,這樣做:
$("#eleID").cursor(); // will return current cursor state for that element
$("#eleID").cursor("pointer"); // will change ele cursor to whatever is ""
$("#eleID").cursor("clear"); // will change ele cursor to default
$("#eleID").cursor("ishover"); // will return boolean of if mouse is over element or not
$("#eleID").cursor("position"); // will return cursor's current position in "relation" to element
也
$.cursor("wait") // will change document cursor to whatever is ""
$.cursor($("#eleID"), "wait"); // same as $("#eleID").cursor("wait");
$.cursor("position") // will return current cursor position
還應該提到,如果您提交多個元素,如$("#eleID1, .elementsWiththisClass")
的「位置」和「isHover」,那麼它將返回就像一個Array
包含對象:
var poses = $("#eleID1, .elementsWiththisClass").cursor("position") // will equal
poses[0] = {
ele: e.fn.e.init[1], // the jquery element
x: XXX, // where XXX is the cursors current position in relation to element
y: XXX
}
poses[1] = { // ...and so on and so forth for each element
您可以檢測使用JavaScript
遊標類型像
<input id="sample_text" name="one" type="text" value="Sample Text" />
和JavaScript代碼應該是這個樣子
$('input[id=sample_text]').click(function() {
alert("test");
var ctl = document.getElementById('sample_text');
var startPos = ctl.selectionStart;
var endPos = ctl.selectionEnd;
alert(startPos + ", " + endPos);
});
你也可以看看這個Jsfiddle for Js Cursor Detection
上面是jQuery代碼寫的,你也可以用香草JS爲您只需將其更改爲
<input id="sample_text" name="one" type="text" value="Sample Text" onclick="detect_cursor()" />
和JavaScript應該看起來像這樣
function detect_cursor() {
alert("test");
var ctl = document.getElementById('sample_text');
var startPos = ctl.selectionStart;
var endPos = ctl.selectionEnd;
alert(startPos + ", " + endPos);
};
這是針對鍵盤光標的。 OP詢問*鼠標*光標。 – 2017-04-22 07:13:37
- 1. 類型檢測
- 2. 檢測類的類型
- 3. 數據類型猜測遊戲
- 4. 檢測異常類型
- 5. json的檢測類型
- 6. Android - 檢測URL MIME類型?
- 7. Python導入類型檢測
- 8. 類型檢測,在Java
- 9. php圖像類型檢測
- 10. 字符類型檢測
- 11. React中的類型檢測
- 12. Elasticsearch - 檢測類型一起
- 13. 從DataHandler檢測MIME類型
- 14. 檢測類型的存在
- 15. 檢測壁紙類型
- 16. TypeScript不檢測泛型類
- 17. 檢測可空類型
- 18. Oracle遊標的返回類型
- 19. 檢測泛型類型是否打開?
- 20. 檢測接口的泛型類型
- 21. 從接口類型檢測類
- 22. 檢測漫遊事件?
- 23. 如何檢測如果類型是另一個泛型類型
- 24. 如何檢測對象類型和標籤
- 25. 如何檢測音頻標籤與音源的差錯類型
- 26. Google Cloud Vision API:檢測徽標類型錯誤
- 27. Selenium 2:檢測鏈接目標的內容類型
- 28. 檢索Oracle遊標與JDBC
- 29. 與類的猜測遊戲
- 30. 在Python中檢測數據類型
你試圖檢測當前的遊標類型,或者只是自己設置?如果你試圖設置它,還有一些其他問題是重複的,這將被關閉。 (如果我是你,我會改變這個問題的意圖,所以它想檢測當前的光標類型,所以它不只是設置光標類型的另一個副本,但是選擇完全取決於你:) – 2011-03-14 21:35:50
但我不'我想要設置的類型,但在頁面中的任何地方.. – mkotwd 2011-03-14 21:42:01
告訴我們爲什麼你想這樣做。我的猜測是,解決方案有一個更簡單的途徑。 FWIW,光標不是DOM的一部分,所以你不能從中獲得更多信息。關於你能做的最好的事情是弄清楚它的位置並從中作出假設。 – 2011-03-16 03:36:35