javascript 单选按钮焦点在 Firefox 上不起作用?
我正在使用一个表单来从用户那里获取信息,并且我还使用了一些单选按钮和文本区域,如果用户没有从单选按钮中选择一个选项,我想聚焦该单选按钮,以便用户可以知道什么数据丢失。 我已经尝试过:
document.FORM.RADIOBUTTON[INDEX].focus();
它实际上在谷歌浏览器中运行良好,但由于某种原因它在火狐浏览器上不起作用, 我尝试使用 setTimeout(.....);
,它确实可以在 google chrome 上工作,但我在 firefox 中没有得到任何东西。 关于如何让它发挥作用有什么想法吗?
I'm using a form to get information from the user, and i am also using some radio buttons and textarea's, if the user hasn't picked an option from the radio buttons i want to focus that radio button so the user can know what data is missing.
I've tried:
document.FORM.RADIOBUTTON[INDEX].focus();
and it actually works well in google chrome, but for some reason it doesn't work on firefox,
i tried to use a setTimeout(.....);
and again, it does work on google chrome but i don't get anything in firefox.
any ideas in how to get this to work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它确实在 Firefox 中有效,但问题是焦点项目在 Firefox 中没有突出显示。如果您尝试使用 Tab 键导航下一个 DOM 元素,您会发现它有效。某些样式可以应用于元素,但 DOM 元素样式也因浏览器而异。请参阅此处的示例
http://jsfiddle.net/uQ3vk/
It indeed works in firefox but the issue is the focused item is not highlighted in firefox. If you try to navigate the next DOM element using tab key, you will find it works. Some styling can be applied to element but DOM element styling also differ from browser to browser. See an example here
http://jsfiddle.net/uQ3vk/
在没有看到 HTML 的情况下,我建议的最佳选择是为您的单选按钮(或至少是您希望能够以编程方式聚焦的按钮)提供一个 ID:
与对 DOM 元素的任何编程访问一样,您的代码不会如果元素尚未解析,则可以工作,因此代码应该位于 document.ready / onload 处理程序中,或者比相关元素晚于源代码中。 (或者在提交处理程序中,假设在页面加载之前不会发生提交。)
Without seeing your HTML the best option I can suggest is to give your radio buttons (or at least the one(s) you want to be able to focus programmatically) an ID:
As with any programmatic access to DOM elements your code won't work if the element(s) haven't been parsed yet, so the code should be either in the document.ready / onload handler or later in the source than the element(s) in question. (Or in a submit handler, assuming a submit won't happen before the page loads.)
我认为 Firefox 中缺少大纲是问题所在,我知道这对我来说是个问题。我添加了一些 CSS 并让它显示出来。
I think the missing outline in Firefox is the issue, I know it was for me. I added some CSS and got it to show.