如何在纯JavaScript中聆听DateTime-Local的紧密事件?

发布于 2025-02-10 04:47:37 字数 1646 浏览 1 评论 0原文

我有类型dateTime-local的输入,其中文本字段被隐藏,showpicker()用于打开Picker GUI的浏览器接口。 showpicker()是通过自定义日历图标的onClick触发的。我想要的是在关闭选择器时运行功能。

我知道您可以通过 Onclose 在他们的datepicker小部件上,但我的项目不使用jQuery,我对添加该框架依赖性不感兴趣。

onclose事件当dateTime-local元素失去焦点时不会触发,因为选择器处于阴影下而不是常规文档层次结构。 onbluronfocusout也无效。

这是我想看到的触发器的最小示例:

const picker = document.getElementById("datePicker");
const icon = document.querySelector("img");    

icon.onclick = function() {
  picker.showPicker();
};

picker.onclose = function() {
  alert("This does not work!");
};

picker.onblur = function() {
  alert("This does not work either!");
};

picker.addEventListener("focusout", (event) => {
  alert("Neither does this!");
});
#datePicker {
  width: 1px;
  height: 1px;
  border: none;
}

img {
  width: 32px;
  height: 32px;
  cursor: pointer;
}
<img src="https://static.vecteezy.com/system/resources/previews/002/387/838/original/calendar-icon-flat-style-isolated-on-white-background-free-vector.jpg" />
<input id="datePicker" type="datetime-local" />

由于security> SecurityError,它在堆栈溢出或JSFiddle上无效,因此有关交叉iFrame,因此要查看行动中的代码,您可能需要在本地运行它。

I have an input of type datetime-local where the text field is hidden and showPicker() is used to open the browser interface for the picker GUI. showPicker() is triggered via an onclick event of a custom calendar icon. What I want is to run a function when the picker is closed.

I know that you can do this in jQuery via onClose on their Datepicker widget, but my project does not use jQuery and I am not interested in adding that framework dependency.

The onclose event does not trigger when the datetime-local element loses focus, due to the picker being in the shadowDOM and not the regular document hierarchy. The onblur and onfocusout did not work either.

Here is a minimal example of what I want to see trigger:

const picker = document.getElementById("datePicker");
const icon = document.querySelector("img");    

icon.onclick = function() {
  picker.showPicker();
};

picker.onclose = function() {
  alert("This does not work!");
};

picker.onblur = function() {
  alert("This does not work either!");
};

picker.addEventListener("focusout", (event) => {
  alert("Neither does this!");
});
#datePicker {
  width: 1px;
  height: 1px;
  border: none;
}

img {
  width: 32px;
  height: 32px;
  cursor: pointer;
}
<img src="https://static.vecteezy.com/system/resources/previews/002/387/838/original/calendar-icon-flat-style-isolated-on-white-background-free-vector.jpg" />
<input id="datePicker" type="datetime-local" />

It doesn't work on Stack Overflow or JSFiddle due to the SecurityError about a cross-origin iframe, so to view the code in action you will likely need to run it locally.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怪我入戏太深 2025-02-17 04:47:37

我能够通过使用onChange事件来使此工作,但是仅当用户实际与弹出窗口交互时。如果他们单击默认的选定日期(今天),则其功能正常,但是如果他们将其放置并关闭弹出窗口,则它不起作用。

请参阅下面的示例代码:

const picker = document.getElementById("datePicker");  

picker.onchange = function() {
  alert("This works when the input is changed, but not when the picker is closed without user interaction.");
};

这比我想要的实际解决方案更像是解决方法,因此欢迎其他答案。

I was able to get this working by using the onchange event, but it only works when the user actually interacts with the popup. If they click the default selected date (today) then it functions fine, but if they just leave it selected and close the popup then it doesn't work.

See example code below:

const picker = document.getElementById("datePicker");  

picker.onchange = function() {
  alert("This works when the input is changed, but not when the picker is closed without user interaction.");
};

This is more of a workaround than the actual solution I was looking for, so other answers are welcome.

镜花水月 2025-02-17 04:47:37

我在寻找解决方案时发现的另一个解决方法是集中精力,然后处理模糊

const picker = document.getElementById("datePicker");  
const icon = document.querySelector("img");    

icon.onclick = function() {
  picker.focus();
  picker.showPicker();
};

picker.onblur = function() {
  alert("Will trigger when either icon/input is focus and click out");
};

Another workaround I found while looking for a solution is to focus and then handle blur

const picker = document.getElementById("datePicker");  
const icon = document.querySelector("img");    

icon.onclick = function() {
  picker.focus();
  picker.showPicker();
};

picker.onblur = function() {
  alert("Will trigger when either icon/input is focus and click out");
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文