- ID
document.getElementById('myElement');
或者document.querySelector('#myElement');
- Class
document.getElementsByClassName('myElement');
或者document.querySelectorAll('.myElement');
- HTML标签
document.getElementsByTagName('div');
或者`.querySelectorAll(‘div’);` - 属性
document.querySelectorAll('[attr-name="val"]');
- Pseudo-class
document.querySelectorAll('#elmem :invalid');
- 子元素
document.getElementById('holder').childNodes;
、document.getElementById('holder').children;
或者document.querySelector('#holder > [disabled]');
- 后代元素`document.querySelectorAll(‘#holder A’);
- 排除 `document.querySelectorAll(‘DIV:not(.exclude)’);
document.querySelectorAll('h1, h2, p');
window.picker = function(selector) { var selectorType = 'querySelectorAll'; if (selector.indexOf('#') === 0) { selectorType = 'getElementById'; selector = selector.substr(1, selector.length); } return document[selectorType](selector); };
不使用JQuery选择HTML Element
最新推荐文章于 2021-06-16 01:57:51 发布