[英]I'm trying to select a canvas element using getElementsByClassName, but all I can get is an array of the elements
(working in ReactJS) (在 ReactJS 中工作)
I am trying to get the correct canvas dom element but I am only able to get an array of elements and I can't seem to use standard array syntax to return the specific element I want to work on.我正在尝试获得正确的 canvas dom 元素,但我只能获得一个元素数组,而且我似乎无法使用标准数组语法来返回我想要处理的特定元素。
import React, {Component} from "react";
import "./Mountaincanvas.scss"
const MountainCanvas = () => {
const canvas = document.getElementsByClassName("mountain-canvas");
console.log(canvas)
console.log(canvas[0])
return (
<canvas className="mountain-canvas"/>
)
}
export default MountainCanvas;
The first time I console log the canvas variable I get a HTMLCollection with a single element我第一次控制台记录 canvas 变量时,我得到一个带有单个元素的 HTMLCollection
HTMLCollection[]
>0: canvas.mountain-canvas
length: 1
__proto__:HTMLCollection
however if I try to just get the first element of the array I get undefined.但是,如果我尝试只获取数组的第一个元素,我会得到未定义的。 How do I select just the first element of the array so I can do things like get Context?我如何 select 只是数组的第一个元素,这样我就可以做诸如获取上下文之类的事情? or simple styling?还是简单的造型?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.