为什么getElementbyId在这种情况下不起作用?

Sar*_*tia 0 javascript html5

好的,这就是代码.当我运行它.它只有h1标签和3个子部分.它不从getElementByID函数中获取值并将其分配给相应的id.

<!Doctype html>
<html>
<head>
  <title>My Playlsit</title>
  <meta charset="utf-8">
  <script>
    function addsongs() {
      var song1 = document.getElementById('song1');
      var song2 = document.getElementById('song2');
      var song3 = document.getElementById('song3');

      song1.innerHTML = "Blue Suede Strings, By Elvis Pagely";
      song2.innerHTML = "Great Objects on Fire, by Jerry JSON Lewis";
      song3.innerHTML =  "I code the Line,by Johnny Javascripts"
    }    
    window.onload = "addsongs";
  </script>    
</head>
<body>
  <h1>My awesome Playlist</h1>
  <ul id="playlist">
    <li id = "song1"</li>
    <li id= "song2"</li>
    <li id = "song3"</li>
  </ul>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

小智 6

你错过了>启动li元素的符号.

此功能也不能指定为字符串值.删除引号.

window.onload = addsongs;
Run Code Online (Sandbox Code Playgroud)