basic dom manipulation Flashcards
creation
var div = document.createElement(‘div’);
addition
document.body.appendChild(div);
style manipulation
div.style.left = “32px”;
change class
div.className = “xxx”;
change id
div.id = “test”;
change inner html
div.innerHTML = “xx”but using jquery would be xx.html(x)
change inner text
div.textContent = “hello”;
removal
div.parentNode.removeChild(div);
get by id
div = document.getElementById(“id”);
get by tags
array = document.getElementByTagName(‘div’);
get by class
array = document.getElementsByClassName(‘class’);
get by css selector (single)
div = document.querySelector(‘div #test’);
get by css selector (multi)
array = document.querySelectorAll(‘div’);
what are text nodes?
hello”hello” is h1’s child textNode. strictly speaking, it’s a node too. :D
sibling (text node indluded)
node = div.nextSibling;