Skip to content

@longmo-utils/browser / $$

Function: $()

ts
function $$(selector): HTMLElement[];

通过 CSS 选择器获取所有匹配的 DOM 元素

Parameters

ParameterTypeDescription
selectorstringCSS 选择器字符串 (例如 '.class', '#id', 'div > p')

Returns

HTMLElement[]

匹配的 DOM 元素数组

Examples

typescript
const buttons = $$('button');
const links = $$('.nav a');
const inputs = $$('input');
typescript
const cards = $$('.card');
cards.forEach(card => {
  card.style.opacity = '0.8';
});
typescript
const images = $$('img');
const largeImages = images.filter(img => {
  const width = parseInt(img.getAttribute('width') || '0');
  return width > 300;
});
typescript
const focusableElements = $$('button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])');

Released under the MIT License.