
javascript (jquery):
$("input").on("change", function() {
this.setAttribute(
"data-date",
moment(this.value, "YYYY-MM-DD")
.format( this.getAttribute("data-date-format") )
)
}).trigger("change")CSS:
input {
position: relative;
width: 150px; height: 20px;
color: white; /* 隐藏原生文本 */
}
input:before {
position: absolute;
top: 3px; left: 3px;
content: attr(data-date); /* 显示格式化后的日期 */
display: inline-block;
color: black;
}
input::-webkit-datetime-edit, input::-webkit-inner-spin-button, input::-webkit-clear-button {
display: none; /* 隐藏原生样式 */
}
input::-webkit-calendar-picker-indicator {
position: absolute;
top: 3px;
right: 0;
color: black;
opacity: 1; /* 显示日历图标 */
}代码解释:
虽然HTML5日期输入框本身不支持直接修改日期格式,但通过JavaScript和CSS的结合,我们可以有效地自定义其显示格式,以满足特定的需求。这种方法不仅可以实现MM/DD/YYYY格式,还可以根据需要自定义为任何其他格式。 在实际应用中,请根据项目需求和用户体验进行适当的调整和优化。
以上就是修改HTML日期输入框默认格式为MM/DD/YYYY的详细内容,更多请关注php中文网其它相关文章!
HTML怎么学习?HTML怎么入门?HTML在哪学?HTML怎么学才快?不用担心,这里为大家提供了HTML速学教程(入门课程),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号