如何解决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 举报,一经查实,本站将立刻删除。