[英]How to access JavaScript element style from styles.CSS, Django
I have this JavaScript code that does createElement, but how can I style it from my separate CSS file?我有这个创建元素的 JavaScript 代码,但是如何从我单独的 CSS 文件中设置它的样式?
JavaScript JavaScript
emails.forEach(function(email) {
const element = document.createElement('div');
element.innerHTML = email.sender + ' ' + email.subject + ' ' + email.timestamp
document.querySelector('#email-container').append(element);
});
HTML HTML
<div id="email-container">
</div>
CSS CSS
#email-container .element{
border-width:2px;
border-style:solid;
border-color:black;
}
if you're styling it that way, then you expect the div to have class element
.如果你是这样设计的,那么你希望 div 有 class element
。 So you just need one extra line to add that class to the element.因此,您只需要额外的一行即可将 class 添加到元素中。 Full code here:完整代码在这里:
emails.forEach(function(email) {
const element = document.createElement('div');
element.classList.add('element');
element.innerHTML = email.sender + ' ' + email.subject + ' ' + email.timestamp
document.querySelector('#email-container').append(element);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.