<!DOCTYPE html>
<html>
<body>
<h1> javascript</h1>
<button type="button" onclick=document.getElementById('demo').innerHTML=Date()">click me to display date and time.</button>
<p id="demo"></p>
</body>
</html>
答案 0 :(得分:4)
几乎没有更正:
<!DOCTYPE html>
<html>
<body>
<h1> javascript</h1>
<!-- added missing qoutes for onclick -->
<button type="button" onclick="document.getElementById('demo').innerHTML= new Date();">click me to display date and time.</button>
<!-- added missing, new keyword for Date() -->
<p id="demo"></p>
</body>
</html>
&#13;
答案 1 :(得分:0)
以下代码将有所帮助......
var date = new Date();
var n = date.toDateString();
var time = date.toLocaleTimeString();
alert(n + ' ' + time);
你甚至可以根据需要将它放在HTML DIV标签中
JS
document.getElementById('time').innerHTML = n + ' ' + time;
HTML
<div id='time'></div>
答案 2 :(得分:0)
onclick=document.getElementById('demo').innerHTML=Date()">click me to display
这需要修复。当您撰写onclick=
时,您开始时没有"
。
简单修复,添加它:
onclick="document.getElementById('demo').innerHTML=Date()">click me to display
答案 3 :(得分:0)
您错过了onclick =&#34; ...&#34;
中的引号<!DOCTYPE html>
<html>
<body>
<h1> javascript</h1>
<button type="button" onclick="document.getElementById('demo').innerHTML=Date()">click me to display date and time.</button>
<p id="demo"></p>
</body>
</html>