是否有JavaScript方法可让html日期输入显示日期选择器? [英] Is there a JavaScript method to make a html date input display the datepicker?
本文介绍了是否有JavaScript方法可让html日期输入显示日期选择器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想通过外部按钮触发日期输入的日期选择器的显示。
I would like to trigger the display of a date input's date-picker from an external button.
<input id="date" type="date" />
<button>display date</button>
例如,如果我有上面的代码,该如何显示日期选择器(出现,并允许您选择一个日期)。我不想使用jQuery或其他库。有没有办法显示来自使用香草JavaScript的外部触发器的本机日期选择器?
For instance if I had the above code, how would I show the date-picker (the box which appears and allows you to pick a date) by clicking the button? I do not want to use jQuery or other libraries. Is there a way to show the native date-picker from an external trigger with vanilla JavaScript?
我正在寻找这样的东西:
I'm looking for something like this:
var button = document.querySelector("button");
button.onclick = () => {
var input = document.querySelector("#date");
input.showDatePicker();
}
推荐答案
这在Firefox和边:
This works well on firefox and edge :
<input id="date" type="date" />
<button>display date</button>
<script type="text/javascript">
var button = document.querySelector("button");
button.onclick = () => {
var input = document.querySelector("#date");
input.focus()
input.click()
}
</script>
这篇关于是否有JavaScript方法可让html日期输入显示日期选择器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文