Skip to content

Instantly share code, notes, and snippets.

@ZoomZhao
Last active December 23, 2016 03:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ZoomZhao/856ce81786aca14e3845124b67caf308 to your computer and use it in GitHub Desktop.
Save ZoomZhao/856ce81786aca14e3845124b67caf308 to your computer and use it in GitHub Desktop.
// 1
const uls = document.getElementByTagName('ul')
for(u of uls) {
u.classList.add('bar')
}
// 2
document.querySelector('#list > li:nth-child(10)').remove()
// 3
const list = document.getElementById('list')
const child = document.querySelector('#list > li:nth-child(500)')
if (child) {
const next = child.nextSibling
const element = document.createElement('li')
element.innerText = '<v2ex.com />'
if (next) {
list.insertBefore(element, next)
} else {
list.appendChild(element)
}
}
// 4
const list = document.getElementById('list')
list.addEventListener('click', function(e) {
let target = e.target
let last = target.closest('#list > li')
/* while (target.id !== 'list') {
last = target
target = target.parentNode
} */
let i = 1;
while (last = last.previousSibling) {
if (last.nodeType === 1) { ++i }
}
alert(i)
}, false)
@fuzhaohao
Copy link

getElementsByTagName 落下s 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment