简体   繁体   English

在javascript中检测当前选中的输入字段的书签

[英]detect currently selected input field in javascript for bookmarklet

I am trying to write a bookmarklet that fills the currently selected field in with a function of today's date. 我正在尝试编写一个小书签,用当前日期的功能填充当前选中的字段。 How do I find the currently tabbed to or selected input field using javascript? 如何使用javascript查找当前选项卡式或选定的输入字段?

To get the active element you can use 要获得活动元素,您可以使用

var input = document.activeElement;

if (input.tagName == "INPUT" || input.tagName == "TEXTAREA")
{
    input.value = "today's date";
}

You can use 您可以使用

document.activeElement

To get the current active element. 获取当前活动元素。

Then you can use it to input the data, like so 然后您可以使用它来输入数据,就像这样

if (document.activeElement.nodeName === 'INPUT') {
   document.activeElement.value = new Date().getTime();
}

But your better off adding a focus event to the element 但是最好将focus事件添加到元素

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM