document.querySelector('a') // 返回找到的第一个,不存在返回 null
document.querySelectorAll('a') // 返回所有,类型是 NodeList。不存在返回长度为 0 的 NodeList
[].slice.call(document.querySelectorAll('a')).forEach(function(el, index){
console.log( index + ": " + el.innerHTML );
})
var newEl = document.createElement('div')
el.cloneNode(true)
el.appendChild(newEl)
el.insertBefore(newEl, el.firstChild)
el.parentNode.insertBefore(newEl, el)
el.parentNode.insertBefore(newEl, el.nextSibling)
el.parentNode.removeChild(el)
el === otherEl
document.querySelector('#my-input').value // 获取
document.querySelector('#my-input').value = 3 // 设置
document.querySelector('input[type=checkbox]').checked
document.querySelector('input[type=checkbox]').checked = true
el.textContent
el.textContent = 'xxx'
el.innerHTML
el.innerHTML = 'xxx'
el.getAttribute('href')
el.setAttribute('href', 'xxx')
el.tagName
el.classList.add(className)
el.classList.remove(className)
el.classList.contains(className) // hasClass
// 注意:此处为了解决当 style 值为 auto 时,返回 auto 的问题
var win = el.ownerDocument.defaultView;
// null 的意思是不返回伪类元素
win.getComputedStyle(el, null).color; // 获取元素的颜色
el.style.color = '#ff0011'
// 与 jQuery 一致(一直为 content 区域的高度)
function getHeight(el) {
const styles = this.getComputedStyle(el);
const height = el.offsetHeight;
const borderTopWidth = parseFloat(styles.borderTopWidth);
const borderBottomWidth = parseFloat(styles.borderBottomWidth);
const paddingTop = parseFloat(styles.paddingTop);
const paddingBottom = parseFloat(styles.paddingBottom);
return height - borderBottomWidth - borderTopWidth - paddingTop - paddingBottom;
}
// 精确到整数(border-box 时为 height 值,content-box 时为 height + padding + border 值)
el.clientHeight;
// 精确到小数(border-box 时为 height 值,content-box 时为 height + padding + border 值)
el.getBoundingClientRect().height;
el.addEventListener('click', function(event){
console.log(this.innerHTML)
})