3

I am trying to "feature detect" IE's behavior when pressing enter in an input box that has a button element next to it (when they are not in a form element).

I'm saying IE's behavior because no one else fires a click event on the next button when pressing the enter-key while the input is focused.

Related question where the first awnser describes why IE behaves like this: IE bug triggers click for 2 buttons?

JS-Fiddle where I try to simulate the key press via jQuery.Event and .trigger: http://jsfiddle.net/DbVrn/

Behavior of said js-fiddle in IE:

  • When opening the page, the input gets focus, and then we try to simulate pressing of the enter-key.
  • The simulated enter-key does nothing, hence the input remains focused and red.
  • If you manually press enter while the input is focused, the button will become focused and green.

The problem i have with my current attempt to detect this feature is that:

$("input").trigger(jQuery.Event("keypress", { which: 13 }));

does not actually do the same as manually pressing the enter-key while the input is focused.

How can I successfully simulate the enter-key so that my test for this behavior is possible? Or is there another way i can test for this behavior?

Edit: Updated title to more clearly state that this needs to be tested via javascript, and that the test needs to work in IE from version 8 to 10. Unless anyone else can provide a way of testing this, I will conclude that I need to use user-agent sniffing to see if browser is IE and choose code-path based off that.

4

3 回答 3

1

似乎没有办法通过 JavaScript 测试这种行为。我已经测试了 IE 8、9 和 10 并确认它们都以这种方式运行。

所以现在,我将结合 Javascript IE 检测的一些想法,为什么不使用简单的条件注释呢?http://tanalin.com/en/articles/ie-version-js/为 IE 创建一个测试,只要 IE 不删除对条件编译注释的支持,它就会可靠地工作。

var ie = (/*@cc_on!@*/false && (function(){
    var div = document.createElement("div"),
        list = div.getElementsByTagName("br"),
        version = 3;
    do {
        div.innerHTML = "<!--[if gt IE " + (++version) + "]><br><![endif]-->";
    } while(list[0]);
    return (version > 4 ? version : 10);
}()));

ie变量将是 Internet Explorer 中的浏览器版本,并将false在其他浏览器中。

于 2013-01-22T17:29:17.333 回答
1

无论是使用 jQuery 的 trigger 方法还是使用本机方法,都可以按照您想要的方式模拟按键。真实和模拟的按键都可以被捕获,但模拟的按键不会触发由真实按键引起的整个事件处理程序链。将这条线放在触发器上方很容易证明这一点

$("input").keypress(function(event) { alert(event.which); });

如您所见,无论是模拟按键还是真实按键,捕获都可以正常工作,而这两种按键的处理方式之间的差异显然仍然存在。

你用你的按键事件对象做什么也没关系。您可以添加一个 keyCode,IE 中真正的按键具有,但这不会改变这一点。似乎什么都不会。不幸的是,我找不到任何解释原因的文档,尽管这个问题已经存在了一段时间

http://forums.asp.net/t/1478871.aspx/1

所以在浏览器中似乎没有办法。你必须从外面做。您可以为此使用 InternetExplorerDriver 之类的东西。

我建议简单地记录哪些用户代理具有此“功能”,而不是功能检测。由于微软通常在向后兼容性方面非常弯曲,因此他们不太可能在未来版本中更改输入字段上的 enter 按键的行为。

http://code.google.com/p/selenium/wiki/InternetExplorerDriver

模拟改变输入/文本区域字段的按键

使用 TextEvent 方法可以在某些浏览器(例如 chrome)中将文本(包括换行符)发送到输入或 textarea 字段,但这在 IE 版本 10 之前的任何版本中都不起作用,如下面的小提琴所示:

http://jsfiddle.net/qz7kV/1/

于 2013-01-21T14:47:21.677 回答
0

我看不到仅从 JavaScript 触发错误的可靠方法。您还有其他几种选择:

  1. 在VM中安装IE并使用UI机器人驱动测试。这需要付出很多努力,但会可靠地触发错误。

  2. 有些公司提供远程测试;他们使用 SSH 隧道访问您身边的服务器,并且可以针对许多不同版本的 IE 测试您的站点。这在技术上很容易设置,但由于公司政策、FUD 和政治的原因,可能很难实现。谷歌“使用多种不同浏览器的测试网站”

  3. 手动测试一次,当它工作时,编写一个测试用例来检查代码是否存在(即当 JavaScript 文件或页面源不包含某个固定字符串时测试失败)。优点:很容易设置,缺点:容易坏

  4. 只需测试一次,然后依靠惯性(即多年来没有人会触及该代码)。

于 2013-01-21T13:31:06.580 回答