简体   繁体   English

JavaScript 如何动态添加嵌套的 html 元素

[英]JavaScript how to dynamically add nested html elements

I have successfully done this with just a span while inserting a text node into the span, but now I need my spans to have checkbox functionality so I need the a label surrounding each span, and a checkbox input before each span.在将文本节点插入跨度时,我仅用一个跨度成功完成了此操作,但是现在我需要我的跨度具有复选框功能,因此我需要每个跨度周围的标签,以及每个跨度之前的复选框输入。

This is how it looks / works hardcoded:这是它的外观/硬编码方式:

Sorry if it was unclear this is the HTML part i'm trying to achieve through JS in a dynamic way抱歉,如果不清楚这是我试图以动态方式通过 JS 实现的 HTML 部分

 <div id="layerList" class="LayerList" style="font-size: 20px;">
    <label>
        <input class="checkbox" type="checkbox"><span class="item item-layer" id="sampleLayer">sample layer 1</span>
    </label>
</div>

CSS associated with the code:与代码相关的 CSS:

.LayerList {
        overflow-y: auto;
        max-height: calc(95% - 60px); /*Has to be 95 so that the last element of span is visible unlike at 100%*/
    }

    .LayerList > .items-container {
        border-top: 1px solid #e7e7e7;
    }

    span:last-child { 
        height: 100%; 
    }

    .LayerList span {
        user-select: inherit;
    }

    input[type="checkbox"]:checked + span {
        border: 1px solid green;
        color: green;
    }
    input[type="checkbox"] {
        visibility: hidden;
    }

This makes it so that when span is clicked it acts as a checkbox.这使得当跨度被点击时它充当一个复选框。 I would like to be able to add these dynamically, through a input box i already have.我希望能够通过我已有的输入框动态添加这些。 I managed to add the span with correct styling and text node, but can't figure out how to wrap the checkbox and span correctly in the label through JS.我设法添加了具有正确样式和文本节点的跨度,但无法弄清楚如何通过 JS 在标签中正确包装复选框和跨度。

My attempt:我的尝试:

function layerCreatorX(submission) {
    let wrapLabel = document.createElement("label") 
    let inCheckBox = document.createElement("input")
    let x = document.createElement("span")
    let t = document.createTextNode(submission)
    inCheckBox.type = "checkbox"
    layerArray.push(submission)
    x.className = "item item-layer"
    x.id = submission 
    x.appendChild(t)
    document.querySelector('.LayerList').appendChild(x)
    wrapLabel.appendChild(inCheckBox)
    document.querySelector('.LayerList').appendChild(inCheckBox)
}

Basically what i'm looking for is for this function to create a label, inside of it input with type "checkbox" and after that a span with textnode submission variable which already works.基本上我正在寻找的是让这个函数创建一个标签,在它的内部输入类型为“checkbox”,然后是一个带有 textnode 提交变量的跨度,它已经可以工作了。

Thanks for any suggestions or help感谢您的任何建议或帮助

Your parenting is just a little off:你的育儿方式有点不对劲:

 function layerCreatorX(submission) { let wrapLabel = document.createElement("label"); let inCheckBox = document.createElement("input"); let x = document.createElement("span"); let t = document.createTextNode(submission); inCheckBox.type = "checkbox"; x.className = "item item-layer"; x.id = submission; x.appendChild(t); wrapLabel.appendChild(inCheckBox); wrapLabel.appendChild(x); document.querySelector('.LayerList').appendChild(wrapLabel); } layerCreatorX("Hello");
 <div id="layerList" class="LayerList" style="font-size: 20px;"></div>

You almost had it right, except you the last two lines add the inCheckBox to the wrapLabel ... then removes it an adds it to the .layerList .您几乎做对了,除了最后两行将 inCheckBox 添加到 wrapLabel ...然后将其删除并将其添加到 .layerList 。

I've named the variables in the following to be representative of the element我在下面命名了变量来代表元素

By the way, your sample has the text as sample layer 1 and the id as sampleLayer - yet these are both, in your code, submission - so ... something is wrong with your example顺便说一句,你的样品有文字sample layer 1和ID为sampleLayer -但这些都是在你的代码, submission -所以...什么是错的你的榜样

Anyway:反正:

function layerCreatorX(submission) {
  layerArray.push(submission);
  
  const label = document.createElement('label');
  
  const input = document.createElement('input');
  input.className = input.type = 'checkbox';
  
  const span = document.createElement('span');
  span.className = 'item item-layer';
  span.appendChild(document.createtextNode(submission));
  span.id = submission;
  
  label.appendChild(input);
  label.appendChild(span);
  
  document.querySelector('.LayerList').appendChild(label);
}

using insertAdjacentHTML ....使用 insertAdjacentHTML ....

function layerCreatorX(submission) {
    const html = `<label>
        <input class="checkbox" type="checkbox"/>
        <span class="item item-layer" id="${submission}">
            ${submission}
        </span>
    </label>`
    document.querySelector('.LayerList').insertAdjacentHTML('before-end', html);
}

Now ... why wouldn't you use that - you can see exactly what you're creating ... in the code现在......你为什么不使用它 - 你可以在代码中准确地看到你正在创建的......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM