DIV使用tabindex获得事件详解 移动div
添加 tabindex='-1' 属性;
默认:获取不到焦点事件(blur)
1 | 
<div class="wl-product" id="wl-product"></div> | 
可以获取焦点事件(blur)
1 | 
<div class="wl-product" id="wl-product" tabindex='-1'></div> | 
具体详解:
The onfocus event occurs when an element receives focus either by the pointing device or by tabbing navigation. 
 This attribute may be used with the following elements: A, AREA, LABEL, INPUT, SELECT, TEXTAREA, and BUTTON.
Those
 elements that do not support the tabindex attribute or support it and
assign it a value of "0" are navigated next. These elements are
navigated in the order they appear in the character stream.
以下来自互联网:
【focus和tabIndex】
在KeyBind程序中,除了绑定对象的keydown事件,还不够的,可以在ff测试下面的代码:
div
style
="width:100px; height:100px; background-color:#CCC;"
onkeydown
="alert(1)"
></
div
>
无论怎样都触发不了onkeydown事件(ie可以触发),那就奇怪了,按照一般的思路应该是可以的啊。
 这个可以从w3c关于KeyboardEvent的部分中找到原因:
 Keyboard events are commonly directed at the element that has the focus.
 大概就是说键盘按键事件一般指向能获取焦点的元素,就是不能获取焦点的元素就不能触发键盘按键事件了。
难道div就不能获取焦点?用下面的代码测试(ff):
div
id
="test"
style
="width:100px; height:100px; background-color:#CCC;"
onfocus
="alert(1)"
></
div
>
<
script
>
document.getElementById(
"
test
"
).focus();
</
script
>
还真的不行,那问题就在于怎么使div能获取焦点了(当然这个是转了不少弯才想出来的)。
最后发现给元素设置tabIndex就能让元素能获取焦点了,如果不需要详细了解的话下面可以略过。
 首先看看w3c关于onfocus的部分:
 The onfocus event occurs when an element receives focus either by the pointing device or by tabbing navigation. 
 This attribute may be used with the following elements: A, AREA, LABEL, INPUT, SELECT, TEXTAREA, and BUTTON. 
 当元素通过指定(点击)或tab导航(Tabbing navigation)获得焦点,onfocus事件就会触发。
 该属性会使用在以下元素(就是说默认可以获取焦点的元素):A, AREA, LABEL, INPUT, SELECT, TEXTAREA, and BUTTON.
 测试下面的代码:
a href
=
"
#
"
onfocus
=
"
alert(1)
"
onkeydown
=
"
alert(2)
"
>
focus
<
/
a>
果然两个事件都可以执行。
接着看Tabbing navigation的部分:
 Those
 elements that do not support the tabindex attribute or support it and
assign it a value of "0" are navigated next. These elements are
navigated in the order they appear in the character stream. 
 这里看得不太明白,关键的意思是给元素设置tabindex为0就可以被导航到了(能获取焦点了)。
 测试下面的代码(ff):
div tabindex
=
"
0
"
style
=
"
width:100px; height:100px; background-color:#CCC;
"
onfocus
=
"
alert(1)
"
onkeydown
=
"
alert(2)
"
><
/
div>
果然两个事件都能触发了。
不过w3c说得很模糊,msdn上倒是很清楚:
 An element can have focus if the tabIndex property is set to any valid negative or positive integer.
 Elements
 that receive focus can fire the onblur and onfocus events as of
Internet Explorer 4.0, and the onkeydown, onkeypress, and onkeyup events
 as of Internet Explorer 5.
 只要元素的tabIndex属性设置成任何有效的整数那么该元素就能取得焦点。元素在取得焦点后就能触发onblur,onfocus,onkeydown, onkeypress和onkeyup事件。
不同tabIndex值在tab order(Tabbing navigation)中的情况:
 Objects with a positive tabIndex are selected in increasing iIndex order and in source order to resolve duplicates.
 Objects with an tabIndex of zero are selected in source order. 
 Objects with a negative tabIndex are omitted from the tabbing order.
 tabIndex值是正数的对象根据递增的值顺序和代码中的位置顺序来被选择
 tabIndex值是0的对象根据在代码中的位置顺序被选择
 tabIndex值是负数的对象会被忽略
这个不知道是否符合标准,但貌似ff跟ie是一样的(不同的地方后面会说)。
 那么设置一个负的tabIndex值应该是最理想的了。
ps:如果对ff的tabindex有兴趣的话,推荐看看Test cases for tabindex bugs in Firefox,里面有更详细更专业的分析。
那ie通过一开始的测试,是不是就说明不需要了呢?我们换一个元素测试:
ul style
=
"
width:100px; height:100px; background-color:#CCC;
"
onfocus
=
"
alert(1)
"
onkeydown
=
"
alert(2)
"
><
/
ul>
换成ul就又不能触发事件了,怎么搞的。
再看看msdn,里面有一段:
 The following elements can have focus by default but are not tab stops. .略. applet, div, frameSet, span, table, td.
 下面的元素默认能获取焦点但不能tab导航:applet, div, frameSet, span, table, td.
 看来ie真是“为程序员着想”,但其他元素总不能漏了啊,还是全部都设置tabIndex好了。
终于回到程序上来,首先设置tabIndex:
=
-
1
;
ff元素获得焦点后会出现一个虚线框,去掉会美观一点:
||
(o.style.outline
=
"
none
"
);
ps:如果tabIndex设为0或以上的话ie也会出现虚线框。
绑定了keydown之后,点击一下容器(获取焦点)后就能用方向键控制方向了,但如果(没有获得焦点时)点击滑块,还是触发不了事件。
 因为滑块在拖动效果中ie的鼠标捕获和ff的取消默认动作导致容器不能获得焦点,那手动设置可以吗?
 是可以的,ff中就是直接在滑块的mousedown事件中执行容器的focus方法获得焦点。
 ie本来也是可以的,但ie中当对象执行focus方法时,如果该对象有部分在滚动条外就会自动滚动到适当的位置(还好点击不会这样)。
 为了降低影响,程序中把滑块也绑定了键盘控制,这样点击滑块时只要执行滑块的focus方法获得焦点就可以了:
oFocus
=
isIE
?
(
this
.KeyBind(
this
.Bar),
this
.Bar) :
this
.Container;
addEventHandler(
this
.Bar,
"
mousedown
"
,
function
(){ oFocus.focus(); });
<script>
    document.getElementById('wl-product').onkeydown=function (ev)
{
    
    var oEvent=ev||event;
    
    //var oDiv=document.getElementById('wl-product');
    //←        37
    //→        39
    
    if(oEvent.keyCode==37)
    {
        
        this.style.left=this.offsetLeft-30+'px';
    }
    else if(oEvent.keyCode==39)
    {
        this.style.left=this.offsetLeft+30+'px';
    }
    
  };
    </script>
DIV使用tabindex获得事件详解 移动div的更多相关文章
- 第三天:JS事件详解-事件流
		
学习来源: F:\新建文件夹 (2)\HTML5开发\HTML5开发\04.JavaScript基础\6.JavaScript事件详解 学习内容: 1)基础概念 2)举例说明: 代码如上,如果用事件 ...
 - JAVASCRIPT事件详解-------原生事件基础....
		
javaScirpt事件详解-原生事件基础(一) 事件 JavaScript与HTML之间的交互是通过事件实现的.事件,就是文档或浏览器窗口中发生的一些特定的交互瞬间,通过监听特定事件的发生,你能 ...
 - 单选框radio改变事件详解(用的jquery的radio的change事件)
		
单选框radio改变事件详解(用的jquery的radio的change事件) 一.总结 1.用的jquery的radio的change事件:当元素的值发生改变时,会发生 change 事件,radi ...
 - DOM——事件详解
		
事件 事件:触发-响应机制 事件三要素 事件源:触发(被)事件的元素 事件名称: click 点击事件 事件处理程序:事件触发后要执行的代码(函数形式) 事件的基本使用 var box = docu ...
 - react第五单元(事件系统-原生事件-react中的合成事件-详解事件的冒泡和捕获机制)
		
第五单元(事件系统-原生事件-react中的合成事件-详解事件的冒泡和捕获机制) 课程目标 深入理解和掌握事件的冒泡及捕获机制 理解react中的合成事件的本质 在react组件中合理的使用原生事件 ...
 - JavaScript事件详解-jQuery的事件实现(三)
		
正文 本文所涉及到的jQuery版本是3.1.1,可以在压缩包中找到event模块.该篇算是阅读笔记,jQuery代码太长.... Dean Edward的addEvent.js 相对于zepto的e ...
 - JavaScript事件详解-Zepto的事件实现(二)【新增fastclick阅读笔记】
		
正文 作者打字速度实在不咋地,源码部分就用图片代替了,都是截图,本文讲解的Zepto版本是1.2.0,在该版本中的event模块与1.1.6基本一致.此文的fastclick理解上在看过博客园各个大神 ...
 - JavaScript事件详解-zepto的事件实现
		
zepto的event 可以结合上一篇JavaScript事件详解-原生事件基础(一)综合考虑源码暂且不表,github里还有中文网站都能下到最新版的zepto.整个event模块不长,274行,我们 ...
 - iOS中—触摸事件详解及使用
		
iOS中--触摸事件详解及使用 (一)初识 要想学好触摸事件,这第一部分的基础理论是必须要学会的,希望大家可以耐心看完. 1.基本概念: 触摸事件 是iOS事件中的一种事件类型,在iOS中按照事件划分 ...
 
随机推荐
- 别名alias
			
alias #查看已设置的别名 alias 别名='原命令' #暂时设定别名(重启失效):alias ls='ls --color=never' unalias 别名 #删除别名 设置别名永久生效 ...
 - Monocular Vision
			
Monocular Vision: a condition in which one eye is blind, seeing with only one eye Binocular Vision: ...
 - 图-用DFS求连通块- UVa 1103和用BFS求最短路-UVa816。
			
这道题目甚长, 代码也是甚长, 但是思路却不是太难.然而有好多代码实现的细节, 确是十分的巧妙. 对代码阅读能力, 代码理解能力, 代码实现能力, 代码实现技巧, DFS方法都大有裨益, 敬请有兴趣者 ...
 - Ubuntu 安装hadoop 伪分布式
			
一.安装JDK : http://www.cnblogs.com/E-star/p/4437788.html 二.配置SSH免密码登录1.安装所需软件 sudo apt-get ins ...
 - JS 实现点击展开菜单
			
1: 获取事件源的两种方式 2: overflow 控制展现 <%@ page language="java" import="java.util.*" ...
 - php 引用其他action中的方法
			
例如要调用IndexAction里的isonline(); 可用下列代码 import('@.Action.IndexAction'); $Index=new IndexAction(); $Inde ...
 - 初始Hibernate框架技术
			
hibernate: 定义:ORM:Object Relational Mapping 对象 关系 映射 使用hibernate时几个必要的: 1.实体类 2.映射文件(类 -数据库表,属性-字段) ...
 - Redis高级实践之————Redis短连接性能优化
			
摘要: 对于Redis服务,通常我们推荐用户使用长连接来访问Redis,但是由于某些用户在连接池失效的时候还是会建立大量的短连接或者用户由于客户端限制还是只能使用短连接来访问Redis,而原生的Red ...
 - 【Todo】【转载】深度学习&神经网络 科普及八卦 学习笔记 & GPU & SIMD
			
上一篇文章提到了数据挖掘.机器学习.深度学习的区别:http://www.cnblogs.com/charlesblc/p/6159355.html 深度学习具体的内容可以看这里: 参考了这篇文章:h ...
 - ID和Name的区别
			
HTML元素的ID和Name属性的区别一直认为ID和NAME是一样的,两个又可以一起出现,甚是疑惑.今天BAIDU了一下,才发现里面大有文章.发出来研究研究:最classical的答案:ID就像是一个 ...