我不知道问题出在哪里,但是这段代码在我的计算机上无法运行。我从我参加过的类(class)(完整的网络开发人员类(class))中复制了它。这在类(class)中效果很好,但对我来说则不然。请有人告诉我我的库存有什么问题。
<!doctype html>
<html>
<head>
<title>Learning Javascript</title>
<meta charset="utf-8" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
</head>
<body>
<button id="textChanger">Change first div text</button>
<div id="firstDiv">This is some text</div>
<button id="textAppender">Append some text</button>
<div id="secondDiv">Javascript is...</div>
<button id="textCreator">Create some text</button>
<div id="emptyDiv"></div>
<script type="text/javascript">
document.getElementById("textChanger").onclick=function() {
document.getElementById("firstDiv").innerHTML="This is
awesome!";
}
document.getElementById("textAppender").onclick=function() {
document.getElementById("secondDiv").innerHTML=document.getElementById("secondDi
v").innerHTML + "great!";
}
document.getElementById("textCreator").onclick=function() {
document.getElementById("emptyDiv").innerHTML="<ul><li>Cat</
li><li>Dog</li></ul>";
}
</script>
</body>
</html>
最佳答案
我认为您的格式有问题。如果您查看字符串,某些文本不会被识别为字符串。
查看这个编辑过的 JavaScript
document.getElementById("textChanger").onclick = function() {
document.getElementById("firstDiv").innerHTML = "This is awesome!";
}
document.getElementById("textAppender").onclick = function() {
document.getElementById("secondDiv").innerHTML = document.getElementById("secondDiv").innerHTML + "great!";
}
document.getElementById("textCreator").onclick = function() {
document.getElementById("emptyDiv").innerHTML = "<ul><li>Cat</li ><li>Dog</li></ul > ";
}
<button id="textChanger">Change first div text</button>
<div id="firstDiv">This is some text</div>
<button id="textAppender">Append some text</button>
<div id="secondDiv">Javascript is...</div>
<button id="textCreator">Create some text</button>
<div id="emptyDiv"></div>
关于javascript - 为什么这个 javascript 根本不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35072780/