如何为 document.activeElement 设置文本值?
知道我的 document.activeElement
是一个输入字段(我不知道组件的确切名称,但可能是 Google 的搜索输入字段),我如何在以编程方式吗?
--update
我在页面加载后通过 javascript 从 xul 应用程序尝试它。粘贴命令工作正常,所以我知道该字段具有焦点。 (我没有放置 Xul 标签,因为它只是关于 javascript)
Knowing that my document.activeElement
is an input field (I don't know exactly the name of the component, but may be the Google's search input field, for example), how can I set a text on it programmatically?
--update
I'm trying it from a xul application, via javascript after the page is loaded. A paste command works fine, so I know the field have the focus. (and I didn't put the Xul tag becouse it's just about the javascript)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
请参阅 mozilla 参考。这与
document.getElementById()
类型相同See the mozilla reference. This is the same type as
document.getElementById()
如果您确定它是一个输入文本字段,只需设置值:
If you are sure it is a input text field, just set the value:
在没有看到您的代码及其运行的上下文的情况下,我只能推测。但是,我的猜测是您正在从 XUL 应用程序调用
document.activeElement
,这意味着document
是 chrome 文档,而不是内容页面。在这种情况下,活动元素可能是您用来显示内容的browser
或iframe
元素。Without seeing your code and the context it is running in, I can only speculate. However, my guess is that you are calling
document.activeElement
from your XUL app, which meansdocument
is the chrome document, not the content page. In this case, the active element is likely to be thebrowser
oriframe
element you are using to display the content.我认为还有一点麻烦,因为我使用的是 Xul 应用程序。 JavaScript 应该像在浏览器中一样工作,但事实并非如此。
我为使其工作所做的是(将内容放入剪贴板后):
I think there's a little more trouble because I'm in a Xul app. Javascript was supposed to work like in the browsers, but it didn't.
What I did to make it work was (after put the content in the clipboard):
如果您希望焦点元素位于相对于给定应用程序窗口的任何位置(例如,它可能位于
元素内),请使用document.commandDispatcher.focusedElement.value
与 document.commandDispatcher.focusedWindow.document.activeElement.value 相同。这将为您提供cmd_paste
操作的元素。If you want the focused element wherever it may be relative to the given application window, e.g. it may be inside a
<browser>
element, usedocument.commandDispatcher.focusedElement.value
which is the same asdocument.commandDispatcher.focusedWindow.document.activeElement.value
. This gives you the element thatcmd_paste
operates on.