document.querySelector使用新创建的div元素返回null

问题描述

我有以下方法,其主要目的是创建带有类的div并使用这些方法为图表创建“容器”。我已在必要的地方发表了评论,以表明这些行的用途。

    createChartContainer(chartRef) {

        // bring up the grid object
        for (var chartDataInTable of this.tableData) {

            // if this grid does not have 0 charts
            if(chartDataInTable.summary_charts.length != 0) {
                // create div
                var createDiv = document.createElement('div');

                // generate a string I can use as the class name for the newly created div shown above
                var randomStringAsClassNames = Math.random().toString(36).substring(7);

                // assign the random string as the className for the newly created div
                createDiv.className = 'div_' + randomStringAsClassNames;
                // chartClassName is defined as a string in data: () => ....
                this.chartClassName = createDiv.className;

                // non-negotiable as this shows the details of the created chart
                var eChart = chartRef.chartElement;

                // select the div that we've just created
                var eParent = document.querySelector("." + this.chartClassName + "");

                eParent.appendChild(eChart);
            }
        }
    },

问题是console.log(eParent)返回null,并且出现以下错误: 未捕获的TypeError:无法读取null的属性'appendChild'

如果有帮助,我正在与ag-grid一起创建图表

因此,总而言之,为什么eParent.appendChild(eChart)无法正常工作?这是因为document.querySelector(“。” + this.chartClassName +“”),但在布局方式上应该正确吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)