JavaScript 节点增删改查
查找节点
前面四个方法除了定义在 document 对象上,还定义在 Element 上,即在元素节点上也可以调用。
querySelector()
document.querySelector
方法接受一个 CSS 选择器作为参数,返回匹配该选择器的元素节点。如果有多个节点满足匹配条件,则返回第一个匹配的节点。如果没有发现匹配的节点,则返回null
。
let el1 = document.querySelector('.myclass');
let el2 = document.querySelector('#myParent > [ng-click]');
Since HTML5 it’s been valid to start an HTML element ID with a number. For example <div id="10">
. From an HTML perspective, that’s fine.