在這個JavaScript代碼就像你可以通過ID刪除項目,它太長了我想要更短的東西。所以我想通過按一個按鈕來刪除所有項目。我試圖編輯它,但我總是失敗。刪除所有項目按鈕
JavaScript代碼:
var currentFormVisibilityStatus = false;
function SaveNotes() {
var category = document.getElementById("slSearchCategory").value;
var todo = document.getElementById("txtToDo").value;
if (category == "") {
alert("Please select Category.");
return;
}
var storage = JSON.parse(localStorage.getItem('ToDoList'));
var arrayLength = storage.length;
storage[arrayLength] = category;
storage[arrayLength + 1] = todo;
localStorage.setItem('ToDoList', JSON.stringify(storage));
category = "";
loadNotes();
clearNote();
}
function clearNote() {
var todo = document.getElementById("txtToDo");
todo.value = '';
}
function loadNotes() {
var storage = JSON.parse(localStorage.getItem('ToDoList'));
if (!storage) {
storage = [];
localStorage.setItem('ToDoList', JSON.stringify(storage));
}
var displayArea = document.getElementById("displayArea");
var currentFilter = document.getElementById("slSearchCategory").value;
var innerDiv = "";
for (var i = storage.length - 1; i >= 0; i = i - 2) {
if (currentFilter == storage[i - 1] || currentFilter == "") {
var todoColor = 'ffffff';
switch (storage[i - 1]) {
case 'Sales':
todoColor = 'ffffff';
break;
default:
todoColor = 'ffffff';
break;
}
innerDiv += "<div class='displayToDo' style='background:#" + todoColor + "'><input type='image' src='delete.png' width='15px' height='15px' onclick='removeMe(" + i + ")' /> " + storage[i] + "</div>"+ "</br>";
}
}
if (innerDiv != undefined) {
displayArea.innerHTML = innerDiv;
}
else {
displayArea.innerHTML = "";
}
}
function removeMe(itemId) {
var storage = JSON.parse(localStorage.getItem('ToDoList'));
storage.splice(itemId - 1, 2);
localStorage.setItem('ToDoList', JSON.stringify(storage));
loadNotes();
}
onload = function() {
loadNotes();
ShowHideForm();
}
HTML代碼:
<div class="headerDiv">
<span>Category :</span>
<select id="slSearchCategory" class="textBox" onchange="loadNotes()" style="width: 100px">
<option value="" selected="selected">All ToDo</option>
<option value="Personal">Personal</option>
<option value="HR">HR Query</option>
<option value="Payroll">Payroll</option>
<option id="Sales" value="Sales">Sales</option>
</select>
<span style="padding-left: 20px">Todo : </span>
<textarea id="txtToDo" class="textBox" rows="2" cols="20" style="width: 300px"></textarea>
<input type="button" onclick="SaveNotes()" name="Submit" class="submitButton" title="Submit"
value="Add Todo" />
</div>
<div id="displayArea">
</div>
<input type="button" onclick="removeAll();" value="Remove all"/>
這不是「Java」的代碼,但「JavaScript的」,他們是與漢堡類似的是漢堡包。我已經改變了你的問題標籤以反映這一點,但你應該對未來的自我負責,研究差異,以免再次迷惑自己。維基百科可以幫助。 –
什麼?有人說它不是Javascript,而是Java,另一個則說它不是Javascript,而是Java。現在我絕對不知道該用什麼。 –
@George Simon誰說它是Java?這絕對是JavaScript。完全不同的東西 –