javascript - 为什么 addEventListener 不适用于 Chrome 中单选按钮的焦点事件?

标签 javascript google-chrome

考虑以下示例(也可在此处获得:http://jsfiddle.net/hq8Fg/1/)。它在 IE9 中工作正常,但在 Chrome 16 中不起作用。

在 IE 中,如果我点击单选按钮,我会看到消息,但在 Chrome 中没有任何反应。

<!DOCTYPE html>
<html>
<head>
<title>Nothing</title>
<script type="text/javascript">
  function begin() {
    var rb1 = document.getElementById('rb1');
    rb1.addEventListener('focus', function() { document.getElementById("theText").value = "focus"; }, false);
  }
</script>
</head>
<body onload="begin()">
    <input id="rb1" type="radio" />
    <textarea id="theText"></textarea>
</body>
</html>

知道为什么它不能在 Chrome 中运行吗?我可以做些什么来修复它?

附注我知道我可能会使用像 JQuery 这样的库,但我想知道是否可以不使用任何库。

最佳答案

根据这个less than stellar source ,单选按钮的焦点事件只应在您 tab 进入时触发,您可以在 fiddle 中看到。

我会看看是否能找到一些更好的文档——最好是 MDN。

编辑

来自 this MDN article

Changing the focus

There are several ways to change the currently focused element. The simplest is to call the focus method of a the XUL element that you wish to set the focus to. The blur method can be used to remove the focus from an element. The following example demonstrates this:

Example 5 : Source View

<button label="Focus" oncommand="document.getElementById('addr').focus()"/> Or, you can use the methods advanceFocus and rewindFocus on the command dispatcher. These methods move the focus to the next element in sequence or the previous element respectively. This is what happens when the user presses TAB or Shift+Tab.

For textboxes, a special attribute, focused is added whenever the element has the focus. You can check for the presence of this attribute to determine if the element has the focus, either from a script or within a style sheet. It will have the value true if the textbox has the focus and, if the textbox does not have the focus, the attribute will not be present.

Suppose you wanted to move the focus from where it currently is, to the next place the browser thinks it should be. A user typically does this by hitting the "Tab" key. You can do this anywhere you have a XUL browser document by simply:

(强调我的)

关于javascript - 为什么 addEventListener 不适用于 Chrome 中单选按钮的焦点事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8714954/

相关文章:

javascript - 如何使用开发者工具查找 Chrome 中的按钮或元素运行的代码

html - 任何好的新的网络开发者浏览器插件? (与过去 6 个月发布的一样)

javascript - 如何让网页浏览器在div滚动到底部后不改变滚动区域?

javascript - 添加自定义函数/方法来测试 Controller t

javascript - 是否使用 phonegap 在 iOS 上禁用水平滚动

javascript - Chrome 开发者工具 : Computed style different from css rule?

javascript - 如何在 Chrome 控制台窗口中开始一个新行?

javascript - 为什么严格模式会使如此简单的操作如此不同?

javascript - ES6 的下划线方法

javascript - 使用闭包使方法在原型(prototype)中私有(private)