访问要删除的元素属性



我想写一个待办事项应用程序。但我被困在删除li元素。我已经开始写代码了,但是我做不到。这个想法是;首先到达element.offsetHeight,因为我认为即使元素名称和它们的类是相同的,它们的偏移量是不同的。所以我想我可以删掉它。但问题是我找不到正确的if语句,我的目标是达到这个:

如果元素的offsetHeight等于被点击的元素(元素本身),则删除该元素。

我也有这个问题:如何到达和点击元素,我不能这样做,因为跨度有相同的类

注意:li中的span需要与事件侦听器一起使用。我的意思是,当我们点击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>

没有必要使用element.offsetHeight来确定用户单击了哪个元素。MouseEvent包含了你需要的所有信息。

通过使用MouseEvent#currentTarget属性,您将可以访问<span/>元素。然后,通过使用element#parentNode,您将可以访问父<li />元素。

const X = document.querySelectorAll(".span")
for (let i = 0; i < X.length; i++) {
let element = X[i]
element.addEventListener('click', function(event) {
const parentListItem = event.currentTarget.parentNode;
if (parentListItem) parentListItem.remove();
});
}
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>

你可以做

const spans = document.querySelectorAll(".span");
spans.forEach(span => {
span.addEventListener("click", (evt) => {
span.parentElement.remove();
});
})

将为每个带有"span"类的项添加一个处理程序。这将删除父节点。

首先,我建议您为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。

最后,您只需要在span上添加一个事件侦听器来删除它的父节点:

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();
});
}

最新更新