javascript - 访问要删除的元素属性
问题描述
我正在尝试编写一个待办事项应用程序。但我坚持删除 li 元素。我已经开始编码,但我无法做到。这个想法是;首先到达,element.offsetHeight
因为我认为即使元素名称和它们的类相同,它们的 offsetHeight 也不同。所以我想我可以删除它。但问题是我找不到正确的 if 语句,我的目标是实现这一目标:
如果元素 offsetHeight 等于单击的元素(元素本身),则删除该元素。
我也有这个问题:How to reach and clicked elements I can't do it because the spans have the same class
注意: li 内的 span 需要与事件侦听器一起使用。我的意思是当我们单击跨度时该函数将触发。
const X = document.querySelectorAll(".span")
console.log(X)
for (let i = 0; i < X.length; i++) {
let element = X[i]
if (element.offsetHeight == element.offsetHeight)
console.log("a")
}
html {
box-sizing: border-box;
background-color: slategray;
}
.graywrap {
width: 100%;
background-color: slateblue;
}
.wrapper {
margin: 0 auto;
width: 40%;
display: flex;
flex-direction: column;
align-items: center;
background-color: #fcfff7;
border-radius: 20px;
}
.add-section {
display: flex;
width: 100%;
flex-direction: row;
align-items: center;
justify-content: space-between;
background-color: #21A0A0;
border-radius: 20px;
border-bottom: 3px solid slategray;
input {
padding: 10px 20px;
text-align: center;
border-radius: 50px;
border: none;
margin: 10px;
box-shadow: 0 0 10px 2px rgba(0, 0, 0, 0.089);
background-color: whitesmoke;
}
}
.btn {
padding: 10px 50px;
text-align: center;
border-radius: 30px;
border: none;
margin: 10px;
background-color: #FFE900;
}
.added-section {
width: 90%;
height: 150px;
background-color: #046865;
margin-top: 20px;
margin-bottom: 20px;
border-radius: 20px;
box-shadow: 0 0 20px 2px rgba(0, 0, 0, 0.062);
opacity: .6;
backdrop-filter: blur(16px) saturate(180%);
-webkit-backdrop-filter: blur(16px) saturate(180%);
border: 2px solid black;
overflow: scroll;
ul {
padding: 0;
}
}
.list-item {
border-bottom: 1px solid #023f3d;
list-style-type: none;
background-color: #035c59;
padding-left: 15px;
box-shadow: 0 0 20px 2px rgba(0, 0, 0, 0.062);
color: whitesmoke;
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: center;
}
.span {
margin-right: 10px;
transition: .5s;
padding: 0px 10px;
padding-top: 2px;
border-radius: 12px;
margin: 5px;
text-align: center;
}
.span:hover {
border: slategray;
background-color: whitesmoke;
color: slategray;
}
<div class="graywrap">
<div class="wrapper">
<div class="add-section">
<input id="inputText" type="text" placeholder="Add an item">
<button id="addİtem" class="btn">Add</button>
</div>
<div class="added-section">
<ul id="list">
<li class="list-item">DKDSKDK <span class="span">X</span></li>
<li class="list-item">DKDSKDK <span class="span">X</span></li>
<li class="list-item">DKDSKDK <span class="span">X</span></li>
</ul>
</div>
<button class="btn" id="clear-all">Clear All</button>
</div>
解决方案
首先,我建议您对 DKDSKDK 使用不同的文本,例如
<li class="list-item">DKDSKDK 1<span class="span">X</span></li>
<li class="list-item">DKDSKDK 2<span class="span">X</span></li>
<li class="list-item">DKDSKDK 3<span class="span">X</span></li>
这将有助于确保删除了正确的任务项。
其次,偏移高度将为您提供元素的高度,据我所知,它们中的所有 3 个的偏移高度均为 20。
最后,您只需要在跨度上添加一个事件侦听器即可删除它的父级:
const X = document.querySelectorAll(".span")
// console.log(X)
for (let i = 0; i < X.length; i++) {
let element = X[i];
element.addEventListener("click",function(){
// this.parentElement.remove(); this refers to the span
element.parentElement.remove();
});
}
推荐阅读
- java - 尝试连接到 AWS Gateway 私有 API 时出现 UnknownHostException
- build - 有没有办法为 OpenDDS 生成代码
- javascript - 运行大 JSON 数据时如何防止冻结或挂起
- javascript - 有效地加载和搜索包含大量文本的网页
- react-native - 动画从 360 度旋转到 0 度 - iOS 传感器
- ios - Xamarin Forms 应用程序上的 iOS 设计器一直说需要 Max,但已成功连接
- reactjs - 将 CopyPlugin 添加到 next.config.js
- netflix-eureka - 功能区从 Eureka 获取服务器列表失败
- python - 自定义 imputer 不适用于 to.numpy ()
- python-3.x - 如何从另一台远程计算机下载文件