javascript - 为什么这个 javascript 根本不起作用?

标签 javascript html function

我不知道问题出在哪里,但是这段代码在我的计算机上无法运行。我从我参加过的类(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/

相关文章:

c++ - 使用函数引用

知道接收器类型的 Perl 函数

javascript - 使用 vue.js 组件和属性调用函数

javascript - Dojo 1.10 - 刷新 TreeView ,同时保持展开的节点状态

javascript - 粘性导航栏在移动设备上存在错误

html - 样式化单个 bsTooltip (shinyBS) 元素

javascript - 页面刷新后 jquery 停止工作

html - 向已有填充的容器内的标题添加填充

javascript - 加载随机图像时如何设置img src标签?

c - 如何使用宏?