Chr*_*nes 6 javascript html5 javascript-events autofocus
在以下示例中,我只获得一个警告框.我读到焦点是在JavaScript代码执行之前.有没有办法让这个工作?
<input id="i" type="text" autofocus onfocus="alert(1)">
<script type="text/javascript">
document.getElementById('i').addEventListener('focus', function() {
alert(2);
}, false);
</script>
Run Code Online (Sandbox Code Playgroud)
(我只在Safari中测试过这个)
编辑:我显然可以这样做(Prototypejs选择器):
var autofocusElement = $$('input[autofocus]')[0];
callListener(autofocusElement);
Run Code Online (Sandbox Code Playgroud)
但与仅添加事件监听器相比,它看起来很丑陋.
编辑:
不要担心自动聚焦属性缺乏浏览器支持.它很容易解决,就像我在下面的链接中所做的那样.我可以看到,这个问题也是最好的解决方案.我的问题是,如果我能够比不必手动调用监听器那么难看.
http://jsfiddle.net/tellnes/7TMBJ/3/
它在Firefox 3.6中运行良好,因为Firefox不支持自动对焦.但是在支持自动对焦的Safari中,并不是所谓的事件.
| 归档时间: |
|
| 查看次数: |
5300 次 |
| 最近记录: |