我如何创建一个选择日期的日历图标,但是在纯JavaScript/html/css中不显示输入?

发布于 2025-02-04 05:43:30 字数 618 浏览 3 评论 0原文

我的目标

可用的内容

” ://i.sstatic.net/gxc5i.png“ alt =“在此处输入图像说明”>

这是使用简单的日历输入完成的。

<input type="date">

我尝试减少输入的宽度,但是随着它在不同的浏览器中变形,它似乎并不优雅。以前的图像在铬上。以下是在Mozilla中出现的方式。

我认为我可以为每个浏览器指定宽度。然而,这似乎不高档而令人费解。

My goal

Calendar pops up when the calendar icon is pressed

What is available

enter image description here

This is done using a simple calendar input.

<input type="date">

I have tried reducing the width of the input, but then it doesn't seem elegant as it deforms in different browsers. The previous images were on chrome. Below is how it appears in Mozilla.

enter image description here

I think I could specify the width for each browser. That however seems inelegant and convoluted.

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

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

发布评论

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

评论(2

如日中天 2025-02-11 05:43:30

大多数现代浏览器您可以使用showpicker() 输入[type = date]元素以使小部件出现。

因此,考虑到这一点,您可以将&lt; input&gt;移动到带有溢出的容器之外:隐藏,然后从中触发小部件。 >事件在另一个元素上。

下面的摘要一样工作的类似,尽管由于有限的支持,我并不建议在公共面对的Web应用程序中使用它:

let cal = document.querySelector('label.calendar');
let ci = document.querySelector('label.calendar input[type=date]');
cal.addEventListener('click', event => ci.showPicker());
label.calendar {
  overflow: hidden;
  user-select: none;
  cursor: pointer;
}

label.calendar input {
  position: absolute;
  bottom: 100%;
  left: 0;
  border: 0;
  padding: 0;
  outline: none;
}
<label class="calendar">
<input type="date">
<span>

In most modern browsers you can use the showPicker() method of an input[type=date] element to make the widget appear.

So, with that in mind, you could move the <input> outside of a container with overflow:hidden and trigger the widget from a click event on another element.

Something like the snippet below might work, though I wouldn't really suggest using this in a public facing web app due to the limited support :

let cal = document.querySelector('label.calendar');
let ci = document.querySelector('label.calendar input[type=date]');
cal.addEventListener('click', event => ci.showPicker());
label.calendar {
  overflow: hidden;
  user-select: none;
  cursor: pointer;
}

label.calendar input {
  position: absolute;
  bottom: 100%;
  left: 0;
  border: 0;
  padding: 0;
  outline: none;
}
<label class="calendar">
  <input type="date">
  <span>????</span>
</label>

Instead, I would recommend you look into other (more mature) options like Smart HTML Calendar which is a Custom HTML Element.

π浅易 2025-02-11 05:43:30

您试图实现的目标是在每个浏览器的一个解决方案中都无法做到的,因为浏览器呈现“日期”输入类型,并且每个人都有自己的有关如何渲染事物的规范。您可以尝试使用自定义组件或插件,该组件或插件使用JavaScript/CSS/HTML呈现日历,这是在不同浏览器中保持一致外观和感觉的唯一方法。

What are you trying to achieve is not possible to do in one solution for every browser because the "date" input type is rendered by the browser and every one of them has their own specification on how to render things. You could try to use a custom component or plugin that renders a calendar using javascript/CSS/HTML that is the only way to have a consistent look and feel across different browsers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文