我有以下方法,主要目的是:能夠創(chuàng)建多個(gè)具有不同類的 div然后我們使用它們?yōu)槎鄠€(gè)圖表創(chuàng)建“容器”問題在于此方法動(dòng)態(tài)創(chuàng)建了 div 和類。我需要在文檔中提供它們才能實(shí)際使用 appendChild,但由于它們是動(dòng)態(tài)創(chuàng)建的,所以我無法將它們硬編碼到文檔中我在必要的地方發(fā)表了評(píng)論,以說明這些線路的用途。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 并且出現(xiàn)以下錯(cuò)誤:Uncaught TypeError: Cannot read property 'appendChild' of null我收到的先前答案是我需要將它放在文檔中,但是當(dāng)我不知道類名是什么時(shí),我將如何在文檔中放置一個(gè)新動(dòng)態(tài)創(chuàng)建的 div 和一個(gè)新動(dòng)態(tài)創(chuàng)建的類名是?
當(dāng)動(dòng)態(tài)創(chuàng)建該元素及其類名時(shí),如何將 div 放入文檔中?
千巷貓影
2023-05-25 17:09:43