DIV焦点事件

时间:2023-03-09 00:32:55
DIV焦点事件

div本来是没有focus和blur事件的。

如果用div来模拟一个input标签,同时需要它和input一样响应focus和blur事件,

就需要给他加上attribute:tabindex

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 .
只要元素的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值是负数的对象会被忽略

obj.style.outline  =   " none "

如果获得焦点的时候出现虚线框可以这样去除。

另外,focus并不会冒泡(w3c标准)