gpt4 book ai didi

javascript - 闭包在循环中不能正常工作

转载 作者:行者123 更新时间:2023-11-30 14:48:58 29 4
gpt4 key购买 nike

我正在尝试创建一个函数 getDelete(),它将创建 onclick 事件 以删除元素 textpointDel。最后一个将由用于创建 3 个元素 pointDel 的支持函数 createPoindetDel() 创建和包含。

我已经尝试了几种方法来解决这个问题,但仍然没有一个正确的结果。我的函数结果仍然是 Uncaught TypeError: Cannot read property 'remove' of undefined 我想删除多个 block 。

我认为问题出在两个函数的闭包上。

附言我必须使用这些结构。我无法简化它。

function createPoindetDel(text) {

for (var i = 0; i < text.length; i++ ) (function(i) {

var textCoords = text[i].getBoundingClientRect();

var pointDel = document.createElement('p');
pointDel.innerHTML = '[x]';
pointDel.className = 'deleting';
pointDel.style.top = textCoords.top + pageYOffset + 'px';
pointDel.style.left = textCoords.right + pageXOffset + 'px';

text[i].appendChild(pointDel);

return pointDel;
})(i);
}

function getDelete() {

var text = document.getElementsByClassName('text-block');

createPoindetDel(text);

var p = document.getElementsByClassName('deleting');

for (var j = 0; j < text.length; j++ ) (function(j) {

p[j].addEventListener( 'click', function() {text[j].remove()} );
p[j].addEventListener( 'click', function() {p[j].remove()} );

})(j);
}

getDelete();
.text-block {
max-width: 500px;
height: auto;
padding: 20px;
border-top: 2px #C4DF9B solid;
background-color: #EDF5E1;
margin-bottom: 0;
}

.text-block span {
font-size: 18px;
font-weight: bold;
color: #111;
margin-top: 0;
}

.text-block p {
font-size: 15px;
margin-bottom: 0;
color: #111;

}

.deleting {
position:absolute;
float:right;
margin:0px;
top: 0;
right: -20px;
display:inline;
cursor:pointer;
}
<!DOCTYPE HTML>
<html>

<head>
<meta charset="utf-8">
<style>
</style>
</head>
<body>
<div class="text-block">
<span>Horse</span>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec.</p>
</div>
<div class="text-block">
<span>Pig</span>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec.</p>
</div>
<div class="text-block">
<span>Mul</span>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec.</p>
</div>

<script>
</script>
</body>
</html>

最佳答案

当您将事件监听器添加到“p”元素时,您可以使用索引:

p[j].addEventListener( 'click', function() {text[j].remove()} );
// you are accessing the j index from the text array

当你删除一个元素时,你改变了数组的长度(文本)。正确的?然后,当您尝试删除另一个元素时,您尝试使用之前定义的索引,例如索引位置 2。但是新数组在位置 2 没有元素。

如果您尝试先点击最后一个“x”,您的代码将毫无问题地运行。因为这样,你会删除最大的索引。

这很难解释。但是你可以尝试使用事件的函数范围来解决:

编辑:请注意,我将 position:relative 放在了您的 .text-block 类中,并在 JS 中评论了您的 position hack。这也将修复您的 CSS。

尝试像这样更改您的事件函数:

function createPoindetDel(text) {

for (var i = 0; i < text.length; i++ ) (function(i) {

var textCoords = text[i].getBoundingClientRect();
var pointDel = document.createElement('p');

pointDel.innerHTML = '[x]';
pointDel.className = 'deleting';
//pointDel.style.top = textCoords.top + pageYOffset + 'px';
//pointDel.style.left = textCoords.right + pageXOffset + 'px';

text[i].appendChild(pointDel);

//return pointDel;
})(i);
}

function getDelete() {
var text = document.getElementsByClassName('text-block');
createPoindetDel(text);
var p = document.getElementsByClassName('deleting');

for (var j = 0; j < text.length; j++ ) (function(j) {
p[j].addEventListener( 'click', function() {
//here 'this' is the element that was clicked, and parentElement you can supose! =)
this.parentElement.remove()
});
})(j);
}

getDelete();
.text-block {
position:relative;
max-width: 500px;
height: auto;
padding: 20px;
border-top: 2px #C4DF9B solid;
background-color: #EDF5E1;
margin-bottom: 0;
}

.text-block span {
font-size: 18px;
font-weight: bold;
color: #111;
margin-top: 0;
}

.text-block p {
font-size: 15px;
margin-bottom: 0;
color: #111;

}

.deleting {
position:absolute;
float:right;
margin:0px;
top: 0;
right: -20px;
display:inline;
cursor:pointer;
}
<!DOCTYPE HTML>
<html>

<head>
<meta charset="utf-8">
<style>
</style>
</head>
<body>
<div class="text-block">
<span>Horse</span>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec.</p>
</div>
<div class="text-block">
<span>Pig</span>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec.</p>
</div>
<div class="text-block">
<span>Mul</span>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec.</p>
</div>

<script>
</script>
</body>
</html>

关于javascript - 闭包在循环中不能正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48458608/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com