【问题标题】:How to put a div into the document when that element and its class name are dynamically created (e.g. document.createElement)?当动态创建该元素及其类名(例如 document.createElement)时,如何将 div 放入文档中?
【发布时间】:2023-04-08 21:41:01
【问题描述】:

我有以下方法,主要目的是:

  1. 能够创建多个具有不同类的 div
  2. 使用这些我们为多个图表创建“容器”

问题是这个方法动态地创建了 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


    【解决方案1】:

    eParent 必须是存在的DOM Element。例如,您可以调用 document.querySelector('body') 并获取正文,然后您可以附加到 body 元素。

    如果没有看到HTML 结构,很难给你一个直接的解决方案。但简而言之,您要附加到的父元素需要在querySelector 函数内。

    【讨论】:

    • 我已经在问题中添加了我的“HTML”结构是什么(在引号中我使用的是 Vue JS),这是否有助于了解我接下来在修复方法方面可以做些什么?
    • 和 Vuetify :D 所以,我仍然不确定你想要这个新元素去哪里,但是为了争论,你希望这些新的 Div 出现在你的 v-card 的底部,例如,您可以将class="div-output" 放在卡片上...然后在您的查询选择器中,.div-output.. 因为该元素现在存在于DOM 中,您的 div 将出现。
    • 您是否考虑过通过列表渲染来输出您的 div 元素? (vuejs.org/v2/guide/list.html)
    • 哦,我认为使用列表来呈现 div 元素可能会起作用!尽管在上面的例子中,类名是使用 createDiv.className = 'div_' + randomStringAsClassNames; 动态生成的。并且 div 是使用 document.createElement('div'); 创建的我将如何尝试创建 div 列表和类?
    • 不,您可以保留相同的随机类生成函数,将其放入您的 Vue 方法中,返回它吐出的随机名称并将该方法 v-bind 到列表渲染循环中的 divs 元素类.像这样...&lt;div v-bind:class="getRandomClass()"&gt; .... &lt;/div&gt; 更多关于绑定类的信息:vuejs.org/v2/guide/class-and-style.html
    猜你喜欢
    • 1970-01-01
    • 2018-10-04
    • 1970-01-01
    • 2019-02-07
    • 1970-01-01
    • 2021-07-07
    • 1970-01-01
    • 2016-01-18
    • 1970-01-01
    相关资源
    最近更新 更多