Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
326 views
in Technique[技术] by (71.8m points)

javascript - type 'NodeListOf<HTMLLIElement>' is not assignable to type 'Element[]'

let li: Element[] = document.getElementsByTagName('span');

I get the type conversion error, how to store the values in 'Element[ ]' ??

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The object returned from document.getElementsByTagName('span') is not compatible with an array object. You need to declare it as following:

let li: NodeListOf<HTMLElement> = document.getElementsByTagName('span');

If you really need this to be an array object you can use:

let li: NodeListOf<HTMLElement> = document.getElementsByTagName('span');
let liArray: Element[] = Array.prototype.slice.call(li);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...