[英]Either my javascript is not working, or the function isn't being called
我的代码应该更改段落的文本。 代码-(.js 和 .html 文件)
<!DOCTYPE html>
<html>
<head>
<title>Java's Crypt</title>
</head>
<body>
<p Id="bello">i will change</p>
<button type="button" onclick="myFunction()">click to change</button>
<script src="crypt.js"></script>
</body>
</html>
(javascript文件)
function myFunction() {
document.getElementById("bello").innerhtml = "toldya";
}
首先在Id
中没有id
<p id="bello">i will change</p>
在 JS 中,您可以使用textContent
或innerText
来更改文本
document.getElementById("bello").textContent = "toldya";
function myFunction() { document.getElementById("bello").textContent = "toldya"; }
<p id="bello">i will change</p> <button type="button" onclick="myFunction()">click to change</button>
根据 Jax-p 的评论, document.getElementById("bello").innerText = "toldya";
应该管用。
此外,最佳做法是为您的 html 属性使用小写字母,例如Id
应该只是id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.