首页 > 解决方案 > javascript onclick是否在段落中显示时间和日期

问题描述

当我点击一个按钮时,我试图让日期出现在一个带有 ID 的空段落中。到目前为止还没有运气。不介意我不能让它出现在段落中,但只是想知道如何在我点击按钮时让它出现,谢谢。
对不起,如果它是一个愚蠢的,
我已经得到了点击的警报,但似乎无法弄清楚这个 在此处输入图像描述

<!DOCTYPE html>
<html>
<head>
<title>Lets Practice Some Code</title>

<link rel="stylesheet" type="text/css" href="/Users/Matteveli/Desktop/javascript practice/style/style.css">
</head>
<body>
<div><h1 id="topTitle"> Time and date Test</h1></div>

<div><button id="alerter">click Me to make an alert pop up.</button></div>

<p id="empty"

></p><div><button id="timeAndDate">click Me to display time and date</button></div>

<script type="text/javascript" src="/Users/Matteveli/Desktop/javascript practice/javascript/funtionality.js"></script>
</body>
<footer>
</footer>
</html>

var alerterButton = document.getElementById("alerter");
var dateButton = document.getElementById("timeAndDate");
var emptyP = document.getElementById("empty");
var d = new Date();

// for the first click that we have working....  " THE ALERT "
//a link to function was called then the function was made
// as below 
alerterButton.onclick = myClickHandler;

function myClickHandler() {alert("the document was clicked")};

/// TIME AND DATE ???

dateButton.onclick = emptyP.innerHTML=d;

function showMeTheDate() {emptyP.innerHTML+d}; 

非常感谢任何帮助。
提前致谢。

标签: javascript

解决方案


dateButton.onclick应该是一个函数。所以,你应该做

dateButton.onclick = () =>  { emptyP.innerHTML= d.getDate() } 

推荐阅读