如何在纯JavaScript中聆听DateTime-Local的紧密事件?
我有类型dateTime-local
的输入,其中文本字段被隐藏,showpicker()
用于打开Picker GUI的浏览器接口。 showpicker()
是通过自定义日历图标的onClick
触发的。我想要的是在关闭选择器时运行功能。
我知道您可以通过 Onclose
在他们的datepicker小部件上,但我的项目不使用jQuery,我对添加该框架依赖性不感兴趣。
onclose
事件当dateTime-local
元素失去焦点时不会触发,因为选择器处于阴影下而不是常规文档层次结构。 onblur
和onfocusout
也无效。
这是我想看到的触发器的最小示例:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我能够通过使用
onChange
事件来使此工作,但是仅当用户实际与弹出窗口交互时。如果他们单击默认的选定日期(今天),则其功能正常,但是如果他们将其放置并关闭弹出窗口,则它不起作用。请参阅下面的示例代码:
这比我想要的实际解决方案更像是解决方法,因此欢迎其他答案。
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:
This is more of a workaround than the actual solution I was looking for, so other answers are welcome.
我在寻找解决方案时发现的另一个解决方法是集中精力,然后处理模糊
Another workaround I found while looking for a solution is to focus and then handle blur