gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:36:52 26 4
gpt4 key购买 nike

考虑以下示例(也可在此处获得: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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com