Javascript 从for循环中的数组中删除项时出现问题
因此,我在for循环中从数组中删除项时遇到问题Javascript 从for循环中的数组中删除项时出现问题,javascript,arrays,loops,Javascript,Arrays,Loops,因此,我在for循环中从数组中删除项时遇到问题 var div=document.querySelector('div'); 变量数组=[ “iPad”, “iPod”, “iPhone” ]; for(设i=0;i
var div=document.querySelector('div');
变量数组=[
“iPad”,
“iPod”,
“iPhone”
];
for(设i=0;i
答复
for(设i=0;i
您使用的是array.splice(array.indexOf(this,1))
错误,此
为您提供单击的元素,当数组包含该元素中的文本时,您需要执行以下操作:
var div=document.querySelector('div');
变量数组=[
“iPad”,
“iPod”,
“iPhone”
];
for(设i=0;i
答复
在onclick
功能中进行以下更改
p.onclick = function() {
array.splice(array.indexOf(this.textContent),1);
this.remove();
console.log(array);
}
请查看
this
至this.innerHTML
var div=document.querySelector('div');
变量数组=[
“iPad”,
“iPod”,
“iPhone”
];
for(设i=0;i
答复
提供的链接不可用working@SyedDaniyalAsif蛋糕是谎言
p.onclick = function() {
array.splice(array.indexOf(this.textContent),1);
this.remove();
console.log(array);
}