javascript 输入类型“time”不带小时,mm:ss格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19398326/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Input type "time" without hours, mm:ss format
提问by user2182051
I would like to know if it's possible to force an input type time without hours, that only shows "00:00" in "mm:ss" format. Is it possible?
我想知道是否可以在没有小时的情况下强制输入类型时间,它只以“mm:ss”格式显示“00:00”。是否可以?
Thanks.
谢谢。
回答by Praveen
The format in max
and min
is wrong
在格式max
和min
是错误的
Try like this
像这样尝试
<input type="time" step='1' min="00:00:00" max="20:00:00">
回答by SidOfc
The problem here is that you might be able to do this with some smart JavaScript but you will find yourself in a tricky situation when it comes to cross browser compatibility, also if you need to support older browsers like IE8, they do not support HTML5 at all.
这里的问题是,您可能可以使用一些智能 JavaScript 来做到这一点,但是当涉及到跨浏览器兼容性时,您会发现自己处于一个棘手的境地,而且如果您需要支持像 IE8 这样的旧浏览器,它们不支持 HTML5全部。
What is it exactly that you want to do?
你到底想要做什么?
For a good plain-JS (<3) fallback I would basically do something simple like this:
对于一个好的纯 JS (<3) 回退,我基本上会做一些简单的事情:
function getCurrentTime() {
//Create a new date object.
var oTime = new Date();
//Get the minutes from the current date object.
var minutes = oTime.getMinutes();
//Get the seconds from the current date object.
var seconds = oTime.getSeconds();
//Optional: Add leading zero's
if (minutes < 10)
minutes = '0'+minutes;
if (seconds < 10)
seconds = '0'+seconds;
//Return the current minutes and seconds
return minutes+':'+seconds;
}
You could create a function that would put the result of the one above in an input field for instance, however this is current time if you seek something else, e.g. a time in the future, this is abit different to accomplish so I won't go there yet :)
例如,您可以创建一个将上述结果放入输入字段的函数,但是如果您寻求其他内容,例如未来的某个时间,则这是当前时间,这与完成有点不同,所以我不会去那里吧:)