Javascript HTMLCollectionOf的字体脚本集css样式<;元素>;,NodeCollection<;元素>;,谷歌自动完成表单
如何为HTMLCollectionOf或NodeCollection设置css样式Javascript HTMLCollectionOf的字体脚本集css样式<;元素>;,NodeCollection<;元素>;,谷歌自动完成表单,javascript,html,css,typescript,Javascript,Html,Css,Typescript,如何为HTMLCollectionOf或NodeCollection设置css样式 let items = document.querySelectorAll('.pac-item') as NodeListOf<HTMLElement>; let items=document.querySelectorAll('.pac item')作为NodeListOf; 对于HTMLElement,设置样式工作良好 typescript定义表明HTMLCollectionOf是HTM
let items = document.querySelectorAll('.pac-item') as NodeListOf<HTMLElement>;
let items=document.querySelectorAll('.pac item')作为NodeListOf;
对于HTMLElement,设置样式工作良好
typescript定义表明
HTMLCollectionOf
是HTMLCollection的扩展,您需要对其进行迭代以访问单个元素
interface HTMLCollectionOf<T extends Element> extends HTMLCollection {
item(index: number): T;
namedItem(name: string): T;
[index: number]: T;
}
扩展HTMLCollection的接口HTMLCollection{
项目(索引:编号):T;
namedItem(名称:字符串):T;
[索引:编号]:T;
}
我希望这有帮助。:) 您使用的是
HtmlCollectionOf
,因此它是一个集合,集合中有许多项。因此,items.style.background
不起作用,因为它属于HtmlElement
换句话说,您必须在集合上循环以应用于每个HtmlElement
。或者你可以申请下面这样的一个,我想这就是你想要做的
items[0]。style.background='#2b2b'代码>谢谢大家!
我的失败…那是元素的集合…通过迭代解决了它
var items:any = document.getElementsByClassName('pac-item');
for (let i = 0; i < items.length; i++) {
let element = items[i];
element.style.background = '#2B2B2B';
element.style.color = '#DEDEDE';
}
var-items:any=document.getElementsByClassName('pac-item');
for(设i=0;i
你不能。迭代它并将其分配给每个元素:)这会为我生成一个TypeScript错误,类型“element”上不存在“style”。如果您只是忽略编译器错误,代码就可以工作,但是如果您想让它消失,则需要使用document.getElementsByClassName('pac-item')作为HTMLCollectionOf