简体   繁体   English

我正在尝试使用 getElementsByClassName 来 select 一个 canvas 元素,但我能得到的只是一个元素数组

[英]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?还是简单的造型?

If you're trying to get a handle to the canvas in the current component, use a ref :如果您尝试在当前组件中获取 canvas 的句柄,请使用ref


const MountainCanvas = () => {
  const canvas = React.useRef();
  
  if (canvas.current) {
    // do stuff with the canvas
  }

  return (
    <canvas ref={canvas} className="mountain-canvas"/>
  )
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在函数中使用 getElementsByClassName 选择所有元素,不起作用 - Select all elements using getElementsByClassName in a function, not working 我试图通过更改宽度来隐藏数组内的所有元素。 在函数内部使用 setInterval 但它只对最后一个元素正确运行 - I'm trying to hide all elements inside the array by changing width. Used setInterval inside function but it only runs correctly for the last element 我正在尝试选择要使用的元素: - I'm trying to select an element I'm working with: (如何)我能否选择一个数组中的所有元素,而不是随机选择math.random选择的元素? - (How) Can I select all elements in an array exept a random an element selected by math.random? 我正在尝试使 canvas 元素随键盘移动 - I'm trying to make the canvas element move with keyboard 如何将多个画布元素放入一个画布元素中? - How can I put multiple canvas elements in to one canvas element? 如何在 HTML 中的元素中设置所有数组元素? - How can I set all array elements in an element in HTML? 我想使用 JavaScript 使用命令提示符选择数组元素,当您选择元素时,该元素更改为 0 - I want to select array elements using command prompt using JavaScript and when you select element that element is change to 0 尝试选择特定元素。 如何将所有其他未选择的元素更改为 false? - Trying to select a specific element. How do I change all other elements that aren't selected to false? 我正在尝试在 javascript 中列出数组的所有 NESTED 值,但只获取数组中每个值的第一个值 - I'm trying to list all NESTED values of an array in javascript, but get only the 1st value of each in the array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM