当输入 \[type="date"\] 处于焦点时如何显示日历弹出窗口

新手上路,请多包涵

有没有办法在输入元素的焦点上激活本机 HTML5 日期选择器下拉菜单?

大输入元件:

大输入 [type=date] 元素

目前,我只能通过单击输入元素最右侧的箭头来使用日历。

大输入元素单击箭头

Large Input[type=date] 元素 onClick of Arrow

我想在输入元素的焦点上激活此日历。

这是有问题的代码。

 <!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Test</title>
  </head>
  <style media="screen">
  .form-question {
    display: flex;
    flex-direction: column;
    justify-content: center;
    margin: 0 0 3rem;
    min-height: 3rem;
  }
  .form-question__title {
    color: #342357;
    font-size: 1.5rem;
    padding: 1rem;
  }
  .input-container {
    border-bottom: solid 2px #333333;
  }
  .input-container input {
    border: none;
    box-sizing: border-box;
    outline: 0;
    padding: .75rem;
    width: 100%;
  }
  </style>
  <body>
    <div class="form-question">
      <div class="form-question__title">
        <span>Effective Date</span>
      </div>
      <div class="input-container">
        <input id="effective-date" type="date" name="effective-date" minlength="1" maxlength="64" placeholder=" " autocomplete="nope" required="required"></input>
        <span class="bar"></span>
      </div>
    </div>
  </body>
</html>

首选 CSS 解决方案,但欢迎使用 javascript,请不要使用 jQuery。

提前致谢!

原文由 MJ12358 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 632
2 个回答

对于任何偶然发现这个问题的人,我通过使 calendar-picker-indicator 成为输入的完整高度和宽度来解决它(只有 webkit firefox 似乎也尊重这一点),如此 概述。

 .input-container input {
    border: none;
    box-sizing: border-box;
    outline: 0;
    padding: .75rem;
    position: relative;
    width: 100%;
}

input[type="date"]::-webkit-calendar-picker-indicator {
    background: transparent;
    bottom: 0;
    color: transparent;
    cursor: pointer;
    height: auto;
    left: 0;
    position: absolute;
    right: 0;
    top: 0;
    width: auto;
}
 <input type="date">

全宽可点击日历下拉菜单

原文由 MJ12358 发布,翻译遵循 CC BY-SA 4.0 许可协议

一线解决方案

<input type="date" onfocus="this.showPicker()">

也适用于类型“time”和“datetime-local”

原文由 Abid Khairy 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
logo
Stack Overflow 翻译
子站问答
访问
宣传栏