• 日期类型的input元素设置默认值为当天


    html5的form元素对日期时间有丰富的支持

    <input type="date">
    <input type="time">
    <input type="datetime">
    <input type="month">
    <input type="week">
    <input type="datetime-local">

    问题是,你使用了它们后,发现日期并不是默认在当天的(没有默认任何日期),而且el.value=new Date()也不行,只支持yyyy-MM-ddTHH:mm:ss.SSS(或其部分)这种形式的的字符串:

    From the documentation:

    A string representing a date.

    Value: A valid full-date as defined in [RFC 3339], with the additional qualification that the year component is four or more digits representing a number greater than 0.

    js默认是没有日期格式化函数的,除非你打算:

    • 引入一个js的日期库
    • 自己写一个简单的日期扩展类(自己考虑每月天数的不同,闰年等等问题,以实现日期的增减)
    • 用后台语言直接在value的位置格式化输出,如<?=date('Y-m-d').'T'.date('H:i') ?>

    这篇文章找到了一个简单的用法,文档在此,使用:

    document.getElementById('datePicker').valueAsDate = new Date();

    有意思的是,我用jQuery的选择器居然不能使用这个属性(设置了后无效)

    此外,这些新玩意你只能在有限场景使用,生产环境是没意义的,浏览器支持太水了。

  • 相关阅读:
    队列
    StringBuilder&StringBuffer 源码阅读
    八种基本类型的包装类
    Object 源码阅读
    String 类源码分析
    SpringBoot 启动流程
    BeanDefinition 实例化过程
    BeanDefinition 解析流程【解析所有配置类】
    BeanDefinition 实例
    切面触发过程
  • 原文地址:https://www.cnblogs.com/walkerwang/p/3613721.html
Copyright © 2020-2023  润新知