2011-03-14 45 views
10

我想要JavaScript代碼來檢測遊標類型。檢測遊標類型

例如,當光標在textarea的從默認文本更改徘徊..

+2

你試圖檢測當前的遊標類型,或者只是自己設置?如果你試圖設置它,還有一些其他問題是重複的,這將被關閉。 (如果我是你,我會改變這個問題的意圖,所以它想檢測當前的光標類型,所以它不只是設置光標類型的另一個副本,但是選擇完全取決於你:) – 2011-03-14 21:35:50

+0

但我不'我想要設置的類型,但在頁面中的任何地方.. – mkotwd 2011-03-14 21:42:01

+1

告訴我們爲什麼你想這樣做。我的猜測是,解決方案有一個更簡單的途徑。 FWIW,光標不是DOM的一部分,所以你不能從中獲得更多信息。關於你能做的最好的事情是弄清楚它的位置並從中作出假設。 – 2011-03-16 03:36:35

回答

0

我想你可以閱讀,就像你可以設置它的cursor CSS屬性,但你必須從一個特定的元素做到這一點因爲AFAIK無法從窗口或文檔對象中讀取光標類型。按照這個邏輯,爲了獲得當前的遊標類型,你必須找到鼠標所在的當前元素,並讀取它的cs。然而,你必須經常檢查光標是否改變,這是緩慢且容易出錯的(通常,你應該總是嘗試將你的代碼放入事件處理程序來對事物作出反應,而不是經常檢查它發生了,並把你的代碼在這個功能更邏輯,高效,健壯,清晰。)

但檢測遊標類型的想法仍然迷住了我,如果有人知道我怎麼會喜歡聽到它。 :d


作爲一個替代的解決方案,而不是讀遊標類型,你爲什麼不只是設置,當它進入,將改變它的元素的事件處理程序?這將會減少很多錯誤,並且可能更直接,因爲我不認爲你對光標太在意,但是如果鼠標輸入了特定的元素。

$("#textbox").mouseover(function() { 
    //I know the cursor has changed because it is now in a textbox 
}); 
+0

但如果沒有規則CSS設置,我能做什麼! – mkotwd 2011-03-14 21:33:44

+0

@mkotwd如果沒有設置css規則,它將是標準光標 - 對於文本框這是'cursor:text;' – Christoph 2012-03-26 16:13:15

3

你能做到這一點,但它不是漂亮,而且很可能會很慢取決於你有多少個元素有你的頁面上。

$('*').mouseenter(function(){ 
    var currentCursor = $(this).css('cursor') ; 

    //do what you want here, i.e. 
    console.log(currentCursor); 
}); 
1

我有一個很好的jQuery的擴展非常適合在此依據這種類型的事情:

https://gist.github.com/2206057

要使用它,這樣做:

$("#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 
3

您可以檢測使用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); 
}; 
+2

這是針對鍵盤光標的。 OP詢問*鼠標*光標。 – 2017-04-22 07:13:37