HTML標籤元素添加獲取焦點事件

The keydown event is sent to an element when the user first presses a key on the keyboard. It can be attached to any element, but the event is only sent to the element that has the focus. Focusable elements can vary between browsers, but form elements can always get focus so are reasonable candidates for this event type.jquery

以上是從jquery文檔中copy下的一段文本,它說明咱們能夠給form元素綁定keydown事件,由於它們能夠獲取焦點,但怎樣去給div、span綁定呢?
答案就是tabindex這個屬性
js中改變這個屬性:jsObj.tabIndex
jquery : $(selector).attr("tabindex",value)網絡

tabindex

 

元素的tabindex屬性用來定義元素是否能夠獲取焦點,是否能夠經過連續的焦點導航(通常狀況是按tab鍵)獲取焦點,與你獲取焦點的順序。
其值必須是整數值。
若是沒有設置,或者設置的值不正確,則按照慣例執行。
若是是負數,用戶不能夠經過連續的焦點導航獲取焦點,但能夠用其餘方式獲取焦點。
若是是零,則能夠經過連續的焦點導航獲取焦點,按照慣例肯定順序。
若是是正數,則能夠經過連續的焦點導航獲取焦點,按照該值肯定順序。this

div默認是得不到焦點的,能夠爲其設置tabindex屬性使其能夠得到焦點。也就能夠綁定鍵盤事件。spa

 

事例 :code

<span id="myspan"></span>orm

js:事件

 $("#myspan").attr("tabindex",0);element

 $("#myspan").focus();文檔

此文章來自網絡轉載
相關文章
相關標籤/搜索