Javascript 将输入内容修改为div
我试图建立一个时钟设置间隔,然后把我们的输入标签,但我不知道为什么是错误的。我知道我可以更改标签上的值。我想也许我可以使用.innerHTML。谢谢。我不确定我是否得到了您想要实现的目标,但要为您的输入字段设置时间,您可以执行以下操作:Javascript 将输入内容修改为div,javascript,html,Javascript,Html,我试图建立一个时钟设置间隔,然后把我们的输入标签,但我不知道为什么是错误的。我知道我可以更改标签上的值。我想也许我可以使用.innerHTML。谢谢。我不确定我是否得到了您想要实现的目标,但要为您的输入字段设置时间,您可以执行以下操作: <!DOCTYPE html> <html> <head> <title>consultation</title> <link rel="stylesh
<!DOCTYPE html>
<html>
<head>
<title>consultation</title>
<link rel="stylesheet" type="text/css" href="css/consultation.css">
<script type="text/javascript">
var date_1;
//setInterval( functionDate() , 999 );
function functionDate()
{
//alert( form_1.length );
date_1 = new Date();
date_1 = date_1.getHours() + ":" + date_1.getMinutes() + ":" + date_1.getSeconds();
form_1.date.value = "";
form_1.date.value = "date_1";
}
</script>
</head>
<body>
<form name="form_1" >
<div id="container">
<div id="header">
<img src="images/picture_1.png" id="image" width="60" height="60">
<p id="select_product_p">
Select product:
</p>
<input list="product" id="select_product">
<datalist id="product" >
<option>Tortilla</option>
<option>Masa</option>
</datalist>
<p id="product_amount_p">
Product amount:
</p>
<input type="number" id="product_amount" min="1">
<p id="unit_price_p">
Unit price:
</p>
<input type="number" id="unit_price" min="1" size="5">
<button type="button" id="add_product">Add Product</button>
<input type="text" name="hour" id="hour_id">
<!--
</div>
<div id="medium">
</div>
<div id="final">
</div>
</div>
-->
</form>
</body>
</html>
我接受你这样的建议:var date_1;var-date_2;setInterval函数日期,999;函数functionDate{//alert form_1.length;date_1=新日期;date_2=date_1.getHours+:+date_1.getMinutes+:+date_1.getSeconds;document.getElementById hour_id.value=date_2;form_1.date.value=date_1;}并得到一个未捕获的TypeError:无法将属性“value”设置为null非常感谢。我的代码没有问题。检查这个JSFiddle:这意味着当您尝试document.getElementByIdhour_id时,它不会加载到您的doom中,因此它为null
var nowDate = new Date();
var timeString = nowDate.getHours() + ":" + nowDate.getMinutes() + ":" + nowDate.getSeconds();
document.getElementById("hour_id").value = timeString;