简体   繁体   English

为什么在添加元素时样式类会从我的元素中删除?

[英]Why are the style classes removed from my element when it's appended?

Can you explain me how it works?你能解释一下它是如何工作的吗? Im trying to append div with content added by innerHTML and it's works, but with no styling classes.我试图 append div 使用由 innerHTML 添加的内容,它可以工作,但没有样式类。

 const el = document.createElement("div"); el.classList.add("col-sm-4", "col-md-3", "py-3"); el.innerHTML = `<div class="card border-primary"> <div class="card-body"> <img src="https://via.placeholder.com/80" class="img-regular"> </div> </div>` document.getElementById('gallery').appendChild(el); // }); // commented out by community
 <div id="gallery"></div>

so... no bootstrap styling and mansory library also not working.所以...没有引导样式和 mansory 库也不起作用。

You've got 2 opening <div> 's and 3 closing </div> s.你有 2 个打开的<div>和 3 个关闭的</div> Try removing the extra closing tag.尝试删除额外的结束标签。

const el = document.createElement("div");
el.classList.add("col-sm-4", "col-md-3", "py-3");
el.innerHTML = `<div class="card border-primary">
  <div class="card-body">
    <img src="${photo.urls.small}" class="img-regular">
  </div>
</div>`
gallery.appendChild(el);

暂无
暂无

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

相关问题 为什么从 p 元素中删除样式后,stylesCombo 的显示不会改变? - Why does the stylesCombo display not change when style is removed from p-element? 分配给element.style时,为什么元素的样式没有更改? - Why isn't my element's style changing when I assign to element.style? 为什么将元素从DOM中删除? - Why is the element removed from the DOM? 为什么从 DOM 中删除元素时注册的事件会消失? - Why do registered events disappear when an element is removed from DOM? 在追加元素后更改元素的样式不会触发从追加元素的样式的转换 - Changing the style of an element after it is appended doesn't trigger transition from the style that it was appended with Javascript-元素的CSS样式在附加时不显示 - Javascript - CSS style for an element does not show when appended 为什么我的元素没有被附加到我的数组状态? - Why is my element not being appended to my array state? 删除元素时,是否删除了使用jquery的数据方法存储的数据? - Is the data stored with jquery's data method removed when the element is removed? 当上部元素从DOM中移除时,如何在元素的position下方平滑地动画在成帧器运动中 - how to animate below element's position smoothly when upper element is removed from DOM In framer Motion 当将元素拖到列表后单词后,元素如何生成可动态内联样式标签时,如何使该元素可恢复 - How to make draggable element as revertable when that element is appended to the list afterwords and element is generating dynamic inline style tag
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM