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

时间:2016-03-17 06:57:44

标签: 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>

1 个答案:

答案 0 :(得分:6)

您错过了启动li元素的>符号。               

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

window.onload = addsongs;