HTML DOM 输入时间值属性
HTML DOM 输入时间值属性返回一个字符串,该字符串是输入时间中 value 属性的值。用户还可以将其设置为新字符串。
语法
以下是语法 −
- 返回字符串值
inputTimeObject.value
- 将 value 属性设置为字符串值
inputTimeObject.value = ‘String’
示例
让我们看一个 输入时间值 属性的示例 −
<!DOCTYPE html> <html> <head> <title>Input Time value</title> <style> form { width:70%; margin: 0 auto; text-align: center; } * { padding: 2px; margin:5px; } input[type="button"] { border-radius: 10px; } </style> </head> <body> <form> <fieldset> <legend>Time-value</legend> <label for="TimeSelect">Time: </label> <input type="time" id="TimeSelect" value="00:00"> <input type="button" onclick="checkForGuess()" value="Guess"> <div id="divDisplay"></div> </fieldset> </form> <script> var divDisplay = document.getElementById("divDisplay"); var inputTime = document.getElementById("TimeSelect"); divDisplay.textContent = 'Guess at what time is the event?'; function checkForGuess() { if (inputTime.value === '18:30') divDisplay.textContent = 'Yes you guessed it right. The event starts at '+inputTime.value; else divDisplay.textContent = 'No you guessed it wrong. Have a go again'; } </script> </body> </html>
输出
这会产生以下输出 −
点击 ‘猜数字’ 按钮,猜错 −
点击 ‘猜数字’ 按钮,猜对 −
广告