所以我遇到了HTML5和javascript的問題。 我爲javascript部分製作了一些.js文件,並且將鏈接與HMTL代碼連接起來,但它不會顯示javascript部分。 這是我的HTMLHTML5和JavaScript的新手,我無法將JavaScript文件連接到HTML文件?
<!doctype html>
<html lang="en">
<head>
<title>Gateway Tunes</title>
<meta charset="utf-8" />
<script src="playlist_store.js"></script>
<script src="playlist.js"></script>
</head>
<body>
<form>
<input type="text" id="songTextInput" size="40" placeholder="Song name">
<input type="button" id="addButton" value="Add Song">
</form>
<ul id="playlist">
</ul>
</body>
</html>
這裏是我的JavaScript文件;該文件的名稱是在頂部
playlist_store.js
function save(item) {
var playlistArray = getStoreArray("playlist");
playlistArray.push(item);
localStorage.setItem("playlist", JSON.stringify (playlistArray));
}
function loadPlaylist() {
var playlistArray = getSavedSongs();
var ul = document.getElementById('playlist");
if (playlistArray !Null) {
for (var i = 0; i < playlistArray.length; i++) {
li.innerHTML = playlistArrray[i];
ul.appenChild(li);
}
}
}
function getSavedSongs() {
return getStoreArray("playlist")
}
function getStoreArray (key);
var playlistArray = localStorage.getItem(key);
if(playlistArray == null || playlistArray == "") {
playlistArray = new Array();
}
else {
playlistArray = JSON.parse (playlistArray);
}
return playlistArray;
}
playlist.js
window.onload = init;
function init() {
var button = document.getElementById ("addButton");
button.onclick = handleButtonClick;
loadPlaylist();
}
function handleButtonClick() {
var textInput = document.getElementById("songTextInput");
var songName = textInput.value;
if (songName == "") {
alert("Button was clicked!");
}
else {
alert("Your track has been added!");
}
var li = document.createElement("li");
li.innerHTML = songName;
var ul = document.getElementById("playlist");
ul.appendChild(li);
save (songName);
}
@Pshemo來這裏發表評論。 –
'var ul = document.getElementById('playlist「);'有錯誤,請將其更改爲單引號或雙引號 – cjd82187
無論您如何編寫這些內容,如果您打算編寫正確語法高亮顯示的編輯器代碼。StackOverflow的基本語法突出顯示單獨應該幾乎告訴你在哪裏你犯了一個錯誤。 –