javascript - 如何使用 javascript 按钮更改标题的文本?
问题描述
菜鸟在这里。我想用一些文本制作一个变量,这样当我点击它时,旧文本就会变成新文本。
<!DOCTYPE html>
<html>
<body>
<h1 id="id01">THis is the old text</h1>
<p>Click on "My Button" to display its value attribute.</p>
<button id="myBtn" name="myname" value="myvalue"
onclick="alert(this.value)">My Button</button>
<p>Click on "Try it" to change the value attribute of "My Button".</p>
<button onclick="myFunction()">Try it</button>
<p><b>Tip:</b> Click on "My Button" before and after you have clicked on
"Try it".</p>
<script>
function myFunction() {
document.getElementById("myBtn").value = "newButtonValue";
document.getElementById("id01").value = "this is the new text";
}
</script>
</body>
</html>
这是我使用的第一个示例,但 var = 会是更好的选择。
解决方案
使用 innerText 而不是值。
function myFunction() {
document.getElementById("myBtn").value = "newButtonValue";
document.getElementById("id01").innerText = "this is the new text";
}
<h1 id="id01">THis is the old text</h1>
<p>Click on "My Button" to display its value attribute.</p>
<button id="myBtn" name="myname" value="myvalue"
onclick="alert(this.value)">My Button</button>
<p>Click on "Try it" to change the value attribute of "My Button".</p>
<button onclick="myFunction()">Try it</button>
<p><b>Tip:</b> Click on "My Button" before and after you have clicked on
"Try it".</p>
推荐阅读
- python - 如果代码出错,如何打印异常?
- python - Twilio 出站状态“进行中”,但音频正在“响铃”
- matlab - 使用 MATLAB 的“imwrite”命令导出时,灰度图像在磁盘上占用的空间比 RGB 更多
- c# - C 语言中的 DataTable 过滤问题(标题中使用的特殊字符)
- angular - Angular - '==' 不包含在 URL 参数中
- html - 如何将变量插入到属性中?
- python-3.x - 无法在容器内执行 efg.py 脚本作为 'arg' 到 kubernetes 部署文件
- firebird - Firebird 最低服务器安装
- sas - SAS 直通设施。如何在查询中从本地表中插入一个大列表?
- java - 问:mapstruct 可以理解表达式返回一个字符串,以便它可以使用我提供的 dateFormat 吗?