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

Why getElementbyId not working in this case?

本文关键字:不起作用 这种情况下 getElementbyId 为什么      更新时间:2023-09-26

好的 这就是代码。当我运行它时。它只有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>

你错过了开始 li 元素的>符号。

此外,函数不能作为字符串值分配。删除引号。

window.onload = addsongs;