1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| function addClassToUl(classText){ document.getElementById('list').classList.add(classText); } addClassToUl('bar');
function addColorToLi(index, color){ Array.from(document.getElementById('list').children)[index].style.backgroundColor = color; }; addColorToLi(2, 'lightgreen'); addColorToLi(4, 'lightblue'); addColorToLi(6, 'lightpink');
function deleteListItem(index) { document.getElementById("list").children[index].remove(); } deleteListItem(10);
function addListItem(index, content){ var newElement = document.createElement('li'); var newContent = document.createTextNode(content); newElement.appendChild(newContent);
var parentElement = document.getElementById('list'); var currentElement = parentElement.children[index];
parentElement.insertBefore(newElement, currentElement); } addListItem(11, 'inserted element');
function clickAlert(){ var parentElement = document.getElementById("list");
function alertTarget(e){ if(e.target && e.target.nodeName === "LI") { alert(`The index is ${Array.from(parentElement.children).indexOf(e.target)}, the content is ${e.target.innerText}`); } }
parentElement.addEventListener('click', alertTarget); } clickAlert();
|