按钮未显示使用JS动态添加到页面
function addElements(mailbox) {
if (mailbox != 'sent') {
let actionDivArchiveBtn = document.querySelector('#actions')
let archiveButton = document.createElement("button");
archiveButton.classList.add('archive');
archiveButton.innerHTML = `<i class="fa fa-archive" aria-hidden="true"></i>`
actionDivArchiveBtn.appendChild(archiveButton);
archiveButton.addEventListener('click', () => {
if (email.archived === false) {
actionDivArchiveBtn.style.color = "blue";
fetch(`/emails/archive/${id}`, {
method: 'PUT',
body: JSON.stringify({
archived: true
})
})
} else {
actionDivArchiveBtn.style.color = "black";
fetch(`/emails/archive/${id}`, {
method: 'PUT',
body: JSON.stringify({
archived: false
})
})
}
})
}
load_mailbox('inbox');
}
<div class="actions">
<span class="action"><i class="fa fa-reply" aria-hidden="true"></i></span>
</div>
<div class="header">
<span class="from">${email.sender}</span>
<span class="date">
<span class="float-right"></span>${email.timestamp}</span>
</div>
<div class="title">${email.subject}</div>
<div class="description">${email.body}</div>
我正在创建一个像单页应用程序这样的gmail,我想使用vanilla javaScript动态创建一个存档按钮(该按钮未显示在我的屏幕上),我想收听用户使用事件侦听器单击“存档”按钮时,请检查如果档案是真实的,则使用put和更改为false,反之亦然。
我遇到的问题是我的按钮没有显示。
我不知道为什么,因为我已经检查了如何创建和附加按钮的类似示例,并采用了类似的过程。请帮助代码审核。
<div class="actions">
<span class="action"><i class="fa fa-reply" aria-hidden="true"></i></span>
</div>
<div class="header">
<span class="from">${email.sender}</span>
<span class="date">
<span class="float-right"></span>${email.timestamp}</span>
</div>
<div class="title">${email.subject}</div>
<div class="description">${email.body}</div>
function addElements(mailbox) {
if (mailbox != 'sent'){
let actionDivArchiveBtn = document.querySelector('#actions')
let archiveButton = document.createElement("button");
archiveButton.classList.add('archive');
archiveButton.innerHTML = `<i class="fa fa-archive" aria-hidden="true"></i>`
actionDivArchiveBtn.appendChild(archiveButton);
archiveButton.addEventListener('click',() => {
if (email.archived === false) {
function addElements(mailbox) {
if (mailbox != 'sent') {
let actionDivArchiveBtn = document.querySelector('#actions')
let archiveButton = document.createElement("button");
archiveButton.classList.add('archive');
archiveButton.innerHTML = `<i class="fa fa-archive" aria-hidden="true"></i>`
actionDivArchiveBtn.appendChild(archiveButton);
archiveButton.addEventListener('click', () => {
if (email.archived === false) {
actionDivArchiveBtn.style.color = "blue";
fetch(`/emails/archive/${id}`, {
method: 'PUT',
body: JSON.stringify({
archived: true
})
})
} else {
actionDivArchiveBtn.style.color = "black";
fetch(`/emails/archive/${id}`, {
method: 'PUT',
body: JSON.stringify({
archived: false
})
})
}
})
}
load_mailbox('inbox');
}
<div class="actions">
<span class="action"><i class="fa fa-reply" aria-hidden="true"></i></span>
</div>
<div class="header">
<span class="from">${email.sender}</span>
<span class="date">
<span class="float-right"></span>${email.timestamp}</span>
</div>
<div class="title">${email.subject}</div>
<div class="description">${email.body}</div>
function addElements(mailbox) {
if (mailbox != 'sent') {
let actionDivArchiveBtn = document.querySelector('#actions')
let archiveButton = document.createElement("button");
archiveButton.classList.add('archive');
archiveButton.innerHTML = `<i class="fa fa-archive" aria-hidden="true"></i>`
actionDivArchiveBtn.appendChild(archiveButton);
archiveButton.addEventListener('click', () => {
if (email.archived === false) {
actionDivArchiveBtn.style.color = "blue";
fetch(`/emails/archive/${id}`, {
method: 'PUT',
body: JSON.stringify({
archived: true
})
})
} else {
actionDivArchiveBtn.style.color = "black";
fetch(`/emails/archive/${id}`, {
method: 'PUT',
body: JSON.stringify({
archived: false
})
})
}
})
}
load_mailbox('inbox');
}
<div class="actions">
<span class="action"><i class="fa fa-reply" aria-hidden="true"></i></span>
</div>
<div class="header">
<span class="from">${email.sender}</span>
<span class="date">
<span class="float-right"></span>${email.timestamp}</span>
</div>
<div class="title">${email.subject}</div>
<div class="description">${email.body}</div>
function addElements(mailbox) {
if (mailbox != 'sent') {
let actionDivArchiveBtn = document.querySelector('#actions')
let archiveButton = document.createElement("button");
archiveButton.classList.add('archive');
archiveButton.innerHTML = `<i class="fa fa-archive" aria-hidden="true"></i>`
actionDivArchiveBtn.appendChild(archiveButton);
archiveButton.addEventListener('click', () => {
if (email.archived === false) {
actionDivArchiveBtn.style.color = "blue";
fetch(`/emails/archive/${id}`, {
method: 'PUT',
body: JSON.stringify({
archived: true
})
})
} else {
actionDivArchiveBtn.style.color = "black";
fetch(`/emails/archive/${id}`, {
method: 'PUT',
body: JSON.stringify({
archived: false
})
})
}
})
}
load_mailbox('inbox');
}
<div class="actions">
<span class="action"><i class="fa fa-reply" aria-hidden="true"></i></span>
</div>
<div class="header">
<span class="from">${email.sender}</span>
<span class="date">
<span class="float-right"></span>${email.timestamp}</span>
</div>
<div class="title">${email.subject}</div>
<div class="description">${email.body}</div>
I am creating a gmail like single page application, I want to create an archive button dynamically using vanilla javascript (the button is not showing on my screen) and I want to listen to when the user clicks on the archive button using event listeners then check if archive is true using PUT and change to false and vice versa.
The problem I am having is that my button is not showing.
I don't know why because I've checked similar examples of how to to create and append buttons and similar process was taken. Please help with a code review.
<div class="actions">
<span class="action"><i class="fa fa-reply" aria-hidden="true"></i></span>
</div>
<div class="header">
<span class="from">${email.sender}</span>
<span class="date">
<span class="float-right"></span>${email.timestamp}</span>
</div>
<div class="title">${email.subject}</div>
<div class="description">${email.body}</div>
function addElements(mailbox) {
if (mailbox != 'sent'){
let actionDivArchiveBtn = document.querySelector('#actions')
let archiveButton = document.createElement("button");
archiveButton.classList.add('archive');
archiveButton.innerHTML = `<i class="fa fa-archive" aria-hidden="true"></i>`
actionDivArchiveBtn.appendChild(archiveButton);
archiveButton.addEventListener('click',() => {
if (email.archived === false) {
function addElements(mailbox) {
if (mailbox != 'sent') {
let actionDivArchiveBtn = document.querySelector('#actions')
let archiveButton = document.createElement("button");
archiveButton.classList.add('archive');
archiveButton.innerHTML = `<i class="fa fa-archive" aria-hidden="true"></i>`
actionDivArchiveBtn.appendChild(archiveButton);
archiveButton.addEventListener('click', () => {
if (email.archived === false) {
actionDivArchiveBtn.style.color = "blue";
fetch(`/emails/archive/${id}`, {
method: 'PUT',
body: JSON.stringify({
archived: true
})
})
} else {
actionDivArchiveBtn.style.color = "black";
fetch(`/emails/archive/${id}`, {
method: 'PUT',
body: JSON.stringify({
archived: false
})
})
}
})
}
load_mailbox('inbox');
}
<div class="actions">
<span class="action"><i class="fa fa-reply" aria-hidden="true"></i></span>
</div>
<div class="header">
<span class="from">${email.sender}</span>
<span class="date">
<span class="float-right"></span>${email.timestamp}</span>
</div>
<div class="title">${email.subject}</div>
<div class="description">${email.body}</div>
function addElements(mailbox) {
if (mailbox != 'sent') {
let actionDivArchiveBtn = document.querySelector('#actions')
let archiveButton = document.createElement("button");
archiveButton.classList.add('archive');
archiveButton.innerHTML = `<i class="fa fa-archive" aria-hidden="true"></i>`
actionDivArchiveBtn.appendChild(archiveButton);
archiveButton.addEventListener('click', () => {
if (email.archived === false) {
actionDivArchiveBtn.style.color = "blue";
fetch(`/emails/archive/${id}`, {
method: 'PUT',
body: JSON.stringify({
archived: true
})
})
} else {
actionDivArchiveBtn.style.color = "black";
fetch(`/emails/archive/${id}`, {
method: 'PUT',
body: JSON.stringify({
archived: false
})
})
}
})
}
load_mailbox('inbox');
}
<div class="actions">
<span class="action"><i class="fa fa-reply" aria-hidden="true"></i></span>
</div>
<div class="header">
<span class="from">${email.sender}</span>
<span class="date">
<span class="float-right"></span>${email.timestamp}</span>
</div>
<div class="title">${email.subject}</div>
<div class="description">${email.body}</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的
QuerySelector
actionDivarchiveBtn
不正确。因为您是通过ID
搜索的,但是在布局中,它是class
,所以我在布局class
中更改为id
,现在它作品。但是,您可能需要class
在布局中,然后将#
更改为。
。Your
querySelector
foractionDivArchiveBtn
is incorrect. Because you search byid
but in layout it isclass
so I changed in layoutclass
toid
and now it works. But may be you needclass
in layout then change#
to.
inquerySelector
: