【发布时间】:2023-04-08 21:41:01
【问题描述】:
我有以下方法,主要目的是:
- 能够创建多个具有不同类的 div
- 使用这些我们为多个图表创建“容器”
问题是这个方法动态地创建了 div 和类。我需要让它们在文档中可用以实际使用 appendChild,但由于它们是动态创建的,我无法将它们硬编码到文档中
我已经在必要的地方发表了评论,以显示这些行的用途。
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);
}
}
},
STRUCTURE OF ELEMENTS IN <script> TAG i.e. the Document (Note: I'm using Vue JS) (e.g. where the div should go)
<template>
<v-card>
<v-text-field
label="Please enter chart name"
v-model="chartName"
append-outer-icon="mdi-content-save"
@click:append-outer="saveChart"
/>
</v-card>
<template>
console.log(eParent) 返回 null,我们遇到以下错误:Uncaught TypeError: Cannot read property 'appendChild' of null
我收到的上一个答案是我需要将它放在文档中,但是当我不知道什么时,我将如何在文档中放置一个新的动态创建的 div 和一个新的和动态创建的类名班级名称是?
【问题讨论】:
标签: javascript css jquery-selectors appendchild createelement