如何在IE9中转换回到标签(焦点更改)?它在IE8工作 [英] How do I convert Enter to Tab (with focus change) in IE9? It worked in IE8

查看:170
本文介绍了如何在IE9中转换回到标签(焦点更改)?它在IE8工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本输入与onkeydown事件处理程序,转换< Enter>到< Tab>通过将事件的keyCode从13更改为9。

I have a text input with an onkeydown event handler that converts <Enter> to <Tab> by changing the event's keyCode from 13 to 9.

<input type="text" onkeydown="enterToTab(event);" onchange="changeEvent(this);" 
       name="" value="" />
<!-- Other inputs exist as created via the DOM, but they are not sibling elements. -->

Javascript:

Javascript:

function enterToTab(myEvent) {
    if (myEvent.keyCode == 13) {
        myEvent.keyCode = 9;
    }
}
function changeEvent(myInput) { var test = "hello"; }

在IE8中,这导致onchange事件触发,但IE9不会发生。相反,输入字段保持聚焦。我怎么能做到这一点? (它在Firefox 3.6和Chrome 10.0中工作。)如果我将文档模式设置为IE8标准,甚至可以在浏览器模式IE9下工作。但是它不适用于IE9标准的文档模式。 (我的文档类型是XHTML 1.0 Transitional。)

In IE8, this caused the onchange event to fire, but that doesn't happen in IE9. Instead, the input field retains focus. How I can I make that happen? (It works in Firefox 3.6 and Chrome 10.0.) This even works in Browser Mode IE9 if I set the Document Mode to "IE8 standards". But it won't work with a Document Mode of "IE9 standards". (My DocType is XHTML 1.0 Transitional.)

由于它在IE7&

请注意:我不能使用输入。 blur()手动设置新焦点,这是我读过的所有其他解决方案建议的。我已经试过onkeypress和onkeyup没有运气。我需要一个通用的解决方案,将导致Web应用程序的字面上的行为,就像我打的< Tab> ;.此外,我没有jQuery,但是,Dojo 1.5是可用的。

Please note: I cannot use input.blur() or manually set a new focus, which is advised by all the other solutions that I've read. I've already tried onkeypress and onkeyup with no luck. I need a generic solution that will cause the web app to literally behave as though I'd hit <Tab>. Also, I don't have jQuery, however, Dojo 1.5 is available to me.

还要注意:我知道这是错误的行为,提交表单。然而,我的客户的工作人员最初来自绿色屏幕环境,其中Enter在字段之间移动它们。我们必须保留相同的UI。这是它是什么。

Also note: I KNOW this is "wrong" behavior, and that Enter ought to submit the form. However, my client's staff originally come from a green screen environment where Enter moves them between fields. We must retain the same UI. It is what it is.

更新:我发现IE8& IE9。在IE8中,我的设置 myEvent.keyCode 成立。在IE9中,它没有。我可以更新 window.event.keyCode ,它会保持,但这不会影响后来发生什么。 Argh ...任何想法?

UPDATE: I found a difference between IE8 & IE9. In IE8, my setting of myEvent.keyCode holds. In IE9, it does NOT. I can update window.event.keyCode, and it will hold, but that won't affect what happens later. Argh... Any ideas?

推荐答案

看起来IE9事件是不可变的。一旦它们被激活,你不能改变它们的属性,只是preventDefault()或取消它们。因此,最好的选择是取消任何输入事件,并从文本输入重新分派新的DOM事件。

Looks like IE9 events are immutable. Once they've been fired you can't change the properties on them, just preventDefault() or cancel them. So you best option is to cancel any "enter" events and re-dispatch a new DOM event from the text input.

示例

function enterToTab(event){
    if(event.keyCode == 13){
        var keyEvent = document.createEvent("Event");
        // This is a lovely method signature
        keyEvent.initKeyboardEvent("onkeydown", true, true, window, 9, event.location, "", event.repeat, event.locale);
        event.currentTarget.dispatchEvent(keyEvent);
        // you may want to prevent default here
    }
}

这里是关于IE9 DOM事件的MSDN文档:

Here's the MSDN documentation around IE9 DOM events:

事件对象 - http://msdn.microsoft.com/en-us/library/ms535863(v = vs.85).aspx

createEvent - http://msdn.microsoft。 com / en-us / library / ff975304(v = vs.85).aspx

createEvent - http://msdn.microsoft.com/en-us/library/ff975304(v=vs.85).aspx

初始化键盘事件 - http://msdn.microsoft.com/en-us/library/ff975297(v=vs.85)。 aspx

initialize a Keyboard Event - http://msdn.microsoft.com/en-us/library/ff975297(v=vs.85).aspx

这篇关于如何在IE9中转换回到标签(焦点更改)?它在IE8工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆