我有以下方法,主要目的是:能夠創建多個具有不同類的 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 和一個新動態創建的類名是?
當動態創建該元素及其類名時,如何將 div 放入文檔中?
千巷貓影
2023-05-25 17:09:43