javascript - 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};
非常感谢任何帮助。
提前致谢。
解决方案
dateButton.onclick
应该是一个函数。所以,你应该做
dateButton.onclick = () => { emptyP.innerHTML= d.getDate() }
推荐阅读
- c# - Emgu/OpenCV: Event for VideoCapture.QueryFrame() finished.
- c# - C#中的泛型类和继承
- ibm-cloud - 如何在应用程序模式下在 IBM CLOUD 上打开 tls 身份验证
- c# - 如何在 Ubuntu 容器中运行使用 MS Access 的可执行文件?
- google-cloud-dataflow - gcloud 数据流组件所需时间
- bash - 使用 bash 查找 PostgreSQL 数据库是否存在
- python - Multiprocessing with IMAP connection
- javascript - Group comments by posts id in an object
- excel - Excel - Two key value lists - Locate where Key is different but value is the same
- typescript - Typescript observable The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type