javascript - 按下垃圾桶按钮时如何删除消息,已删除的消息不应再在视图中可见
问题描述
我添加了一个垃圾按钮,按下该按钮会删除整个消息框/框架,并且此消息存储在一个切换按钮(已删除消息:) 中,单击该按钮应切换视图以仅显示已删除的消息。如何做到这一点?
HTML:
消息查看器 <div><h2 id='starCounter' >Starred:</h2></div>
<div><h2 id='trashCounter' >Trashed Messages:</h2></div>
<figure class=frame id="box">
<span class="helper"></span><img class= "profile1" src="widows2.jpg" alt="my img"/>
<p>Twitter | Feb, 2017 <br><br>
Message 1 from person Name. His Favourite music genre is pop</p>
<button class="trash_btn" id="btn2" >Trash</button>
<button class="btn-hold" id="btn1" data-text-swap=" Starred!">Star Message!</button>
<figcaption id="text">Name</figcaption>
</figure>
CSS:
.helper {
display: inline-block;
height: 95%;
}
.frame {
height: 100px;
width: 100px;
white-space: nowrap;
text-align: center;
margin: 1em 0;
padding: 20px;
float: left;
}
.profile1 {
background: #3A6F9A;
float: left;
vertical-align: left;
max-height: 150px;
max-width: 150px;
justify-content: space-around;
text-align: justify;
width: [width of img];
}
#box {
width: 1460px;
justify-content: space-around;
height: 150px;
box-sizing: 15px;
border: 1px solid;
}
JavaScript:
var count;
var star_count =0;
var button = document.querySelectorAll(".frame button")[1];
button.addEventListener('click', function() {
if(this.classList.contains('btn-alt-color')){
this.classList.remove('btn-alt-color');
star_count--;
}
else {
this.classList.add('btn-alt-color');
star_count++;
}
console.log(star_count);
document.getElementById("starCounter").innerText = "Starred: " + star_count;
if (button.getAttribute("data-text-swap") == button.innerHTML) {
button.innerHTML = button.getAttribute("data-text-original");
} else {
button.setAttribute("data-text-original", button.innerHTML);
button.innerHTML = button.getAttribute("data-text-swap");
}
}, false);
解决方案
您需要在单击 thrash 按钮时删除 dom 元素。将 html div 包装在父 div 中,并从按钮和图形中删除重复的 id。
下面是运行示例
html代码-
<div>
<h2 id='starCounter'>Starred:</h2>
</div>
<div>
<h2 id='trashCounter'>Trashed Messages:</h2>
</div>
<div id="parent">
<figure class=frame >
<span class="helper"></span><img class="profile1" src="widows2.jpg" alt="my img" />
<p>Twitter | Feb, 2017 <br><br>
Message 1 from person Deep. His Favourite music genre is pop</p>
<button class="trash_btn" onclick="thrashMe(this)">Trash</button>
<button class="btn-hold" id="btn1" data-text-swap=" Starred!">Star Message!</button>
<figcaption id="text">Deep Mehta</figcaption>
</figure>
<figure class=frame >
<span class="helper"></span>
<img class="profile1" src="man-how-to.jpg" alt="my img" />
<p>Twitter | Feb, 2017 <br><br>
Message 1 from person Deep. His Favourite music genre is pop</p>
<button class="trash_btn" onclick="thrashMe(this)">Trash</button>
<button class="btn-hold" data-text-swap="Starred!">Star Message!</button>
<figcaption id="text">Tushar Kamble</figcaption>
</figure>
<figure class=frame >
<span class="helper"></span><img class="profile1" src="download.jpg" alt="my img" />
<p>Twitter | Feb, 2017 <br><br>
Message 1 from person Deep. His Favourite music genre is pop</p>
<button class="trash_btn" onclick="thrashMe(this)">Trash</button>
<button class="btn-hold" data-text-swap="Starred!">Star Message!</button>
<figcaption id="text">Parth Jaiswal</figcaption>
</figure>
<figure class=frame >
<span class="helper"></span><img class="profile1" src="images.jpg" alt="my img" />
<p>Twitter | Feb, 2017 <br><br>
Message 1 from person Deep. His Favourite music genre is pop</p>
<button class="trash_btn" onclick="thrashMe(this)">Trash</button>
<button class="btn-hold" data-text-swap="Starred!">Star Message!</button>
<figcaption id="text">Arnab Tarwani</figcaption>
</figure>
</div>
JavaScript 代码-
<script>
function thrashMe(obj) {
document.getElementById("parent").removeChild(obj.closest("figure"));
}
</script>
推荐阅读
- r - 使用 Tidyverse 计算 REDCap 数据中特定字符串在多列中出现的频率
- swift - 如何从 Locale 获取本地化代码?
- kotlin - Kotlin 中出现意外标记(使用 ';' 分隔同一行上的表达式)错误
- css - 如何在 React JS 中使用 Google Fonts API?
- javascript - YouTube Data API Insert 方法缺少什么参数
- c++ - 找不到 C++ CodeBlocks 和 wxWidgets 文件?
- log4j - log4j2.8.2.jar 是否适用于 JDK 1.7。如何找到 jar 文件支持的 jdk 版本?
- deep-learning - 我可以对以下图像使用哪些异常检测算法(监督或非监督)?
- python - 气流中的 ImportError:没有名为 acme.operators.dwh_operators 的模块
- azure - 无法在 Ubuntu 18.04 上安装 Azure-cognitiveservices-speech