gpt4 book ai didi

javascript - 如何单击按钮并更改先前显示的文本( html )

转载 作者:行者123 更新时间:2023-11-28 06:14:00 25 4
gpt4 key购买 nike

我正在尝试创建一个按钮,单击该按钮会将之前显示的文本更改为不同的内容。我已经创建了一个测试代码,但无法弄清楚。我想知道是否有人可以帮忙?我需要使用 JavaScript。虽然此代码在单击时显示时间,但我需要它在单击时显示文本。我不知道如何修改此代码以满足我的需要

我在需要帮助的地方添加了内部评论。

<!DOCTYPE html>
<html>
<body>



<p id="demo" onclick="myFunction()">Test</p>

<script>
function myFunction() {
document.getElementById("demo").innerHTML =;
}
</script>


#need to change the code below to display text and not time

<button type="button"
onclick="document.getElementById('demo').innerHTML = Date()">
Click me to display Date and Time.</button>

<p id="demo"></p>

</body>
</html>

最佳答案

如果您希望它显示一些文本,请向您的函数添加一个字符串。像这样的事情会起作用:

function myFunction() {
document.getElementById("demo").innerHTML ="Some text";
}

看看它在这个 JS Fiddle 中的工作:https://jsfiddle.net/r22jaffb/

希望有帮助。

关于javascript - 如何单击按钮并更改先前显示的文本( html ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36133483/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com