boundingBoxCollection 长度为 0

问题描述 投票:0回答:1
    const { selectedButton } = this.state;

    var boundingBoxCollection = [];
    console.log(boundingBoxCollection.length);

    var boundingBoxes = document.querySelectorAll('.cropper-face');
    console.log("I am ice"+boundingBoxes.length);

    if(boundingBoxes.length > 0){
      boundingBoxes.forEach(elem => {
        if(!boundingBoxCollection.includes(elem)){
          boundingBoxCollection.push(elem);
        }
      });
    }

我的boundingBoxCollection未更新

解决这个问题,我希望更新我的数组

javascript reactjs dom
1个回答
0
投票
const { selectedButton } = this.state;

// Ensure boundingBoxCollection is initialized properly
var boundingBoxCollection = Array.from(document.querySelectorAll('.cropper-face'));

console.log("Initial length: " + boundingBoxCollection.length);

// Loop through the selected elements and push them into boundingBoxCollection
var boundingBoxes = document.querySelectorAll('.cropper-face');
console.log("I am ice" + boundingBoxes.length);

if (boundingBoxes.length > 0) {
  boundingBoxes.forEach(elem => {
    if (!boundingBoxCollection.includes(elem)) {
      boundingBoxCollection.push(elem);
    }
  });
}
© www.soinside.com 2019 - 2024. All rights reserved.