ts中document.querySelector报错如何处理呢

ts中document.querySelector报错如何处理呢?感谢!

ParentNode.querySelector<"body">(selectors: "body"): HTMLBodyElement | null (+2 overloads)
Returns the first element that is a descendant of node that matches selectors.

document.querySelector('body').appendChild(txt);
document.querySelector('#' + copyid).select();
window.getSelection().removeAllRanges();
阅读 4.7k
1 个回答

因为你用querySelector去获取dom的时候可能为空,所以需要使用断言:
(document.querySelector('body') as HTMLElement).appendChild(txt);

推荐问题