奇怪的杯子

          年輕沒有失敗、

           

          DOM元素如何獲得焦點

          首先讓我們看看哪些元素可以直接獲得焦點element..focus()

          // Form : http://www.w3.org/TR/html5/editing.html#focusable

          • a elements that have an href attribute
          • link elements that have an href attribute
          • button elements that are not disabled
          • input elements whose type attribute are not in the Hidden state and that are not disabled
          • select elements that are not disabled
          • textarea elements that are not disabled
          • command elements that do not have a disabled attribute
          • Elements with a draggable attribute set, if that would enable the user agent to allow the user to begin a drag operations for those elements without the use of a pointing device
          • Each shape that is generated for an area element

          而除上面以外的元素(比如:div,p)一般都無法直接獲得焦點,那如何處理呢?

          1. 給元素添加 contenteditable 屬性。

            // From : http://www.w3.org/TR/2009/WD-html5-20090423/editing.html#attr-contenteditable

            User agents must make editing hosts focusable (which typically means they enter the tab order).

          2. 給元素添加 tabindex 屬性。

            // From : http://www.w3.org/TR/2009/WD-html5-20090423/editing.html#attr-tabindex

            The tabindex content attribute specifies whether the element is focusable, whether it can be reached using sequential focus navigation, and the relative order of the element for the purposes of sequential focus navigation.

            非常推薦使用tabindex = -1 ,基本無副作用!!

            // From : http://www.w3.org/TR/2009/WD-html5-20090423/editing.html#attr-tabindex

            If the value is a negative integer: The user agent must allow the element to be focused, but should not allow the element to be reached using sequential focus navigation.

          綜述,使無法直接獲得焦點的元素獲得焦點的最佳實踐就是:給元素添加tabindex = -1

          posted on 2012-01-18 09:49 奇怪的杯子 閱讀(168) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          導航

          統計

          常用鏈接

          留言簿

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 宁陕县| 永修县| 闽侯县| 汨罗市| 高密市| 大洼县| 凤城市| 新乡市| 额尔古纳市| 东丰县| 冷水江市| 棋牌| 鱼台县| 凤翔县| 临夏县| 鸡泽县| 铁力市| 尉犁县| 宜兰县| 德令哈市| 育儿| 万宁市| 云阳县| 徐州市| 遵义县| 呼玛县| 富宁县| 始兴县| 霸州市| 天门市| 金门县| 太白县| 东光县| 浦城县| 皋兰县| 廊坊市| 南和县| 繁峙县| 通河县| 海阳市| 睢宁县|