xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

Web API Document All In One

Web API Document All In One

document

document;

https://developer.mozilla.org/en-US/docs/Web/API/Document

document.all ❌

已弃用

typeof document.all
// 'undefined'

document.all;

https://developer.mozilla.org/en-US/docs/Web/API/Document/all

TypeScript

HTMLElement / HTMLCollectionOf / NodeListOf


  // const dom = document.querySelector(selector);
  // const dom: Element | null
  // ❌
  // const dom = document.querySelector(selector) as Element;
  // ✅
  // const dom = document.querySelector<HTMLElement>(selector);
  // ✅
  // const dom = <HTMLElement>document.querySelector(selector);
  // ✅
  const dom = document.querySelector(selector) as HTMLElement;
  // console.log('\nbackground =', background);
  // console.log('\ndom =', dom);
  if (dom) {
    dom.style.background = background;
  }
  // ✅
  const doms = document.querySelectorAll(selector) as NodeListOf<HTMLElement>;
  // ✅
  // const doms = document.querySelectorAll(selector) as HTMLCollectionOf<HTMLElement>;
  // ❌
  // const doms = (NodeList<HTMLElement>)document.querySelectorAll(selector);
  // ❌
  // const doms = (HTMLCollection<HTMLElement>)document.querySelectorAll(selector);
  if (doms) {
    doms[0].style.background = background;
  }

https://www.typescriptlang.org/docs/handbook/dom-manipulation.html

https://github.com/microsoft/TypeScript/blob/main/lib/lib.dom.d.ts

refs



©xgqfrms 2012-2020

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2022-04-27 11:04  xgqfrms  阅读(36)  评论(2编辑  收藏  举报