简体   繁体   English

jQuery追加合并div

[英]jQuery append merging divs

I've got this simple HTML element: 我有这个简单的HTML元素:

<div class="progress hidden-tmp" id="progress-template">
    <div class="progress-bar progress-bar-striped active" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100"></div>
</div>

I then proceed to load this as jQuery element: 然后,我将其加载为jQuery元素:

const TEMPLATE = '#progress-template';

constructor(name, parent) {
    this.name = name;

    /** @type {jQuery|Element} */
    this.element = $(TEMPLATE).clone().attr('id', 'progress-' + name);
    this.moveTo(parent);
}

Later I append this element to "parent", which is another jQuery element #example: 稍后,我将此元素附加到“ parent”,这是另一个jQuery元素#example:

/**
 * Puts progressbar to parent element and set it visible
 * @param {jQuery|Element} parent
 */
moveTo(parent) {
    this.element.detach();
    console.log(this.element.prop('outerHTML'));
    parent.append(this.element);
    this.element.show();
}

If I run the above, the console.log shows this (which is correct): 如果我运行以上命令,console.log将显示此信息(正确):

<div class="progress hidden-tmp" id="progress-myBar">
    <div class="progress-bar progress-bar-striped active" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100"></div>
</div>

But this is what is appended to parent div: 但这是附加到父div的内容:

<div class="progress hidden-tmp progress-bar-striped active" id="progress-myBar" style="display: block; width: 25%;" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100"></div>

It appears that both divs in element got merged into one. 看来element中的两个div都合并为一个。 Any ideas why? 有什么想法吗?

My mistake, I've forgot there is one line that was also playing with inner HTML of that element. 我的错误,我忘记了还有一行正在使用该元素的内部HTML。 So append() works perfectly fine. 因此append()可以很好地工作。

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

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