Javascript 从for循环中的数组中删除项时出现问题

Javascript 从for循环中的数组中删除项时出现问题,javascript,arrays,loops,Javascript,Arrays,Loops,因此,我在for循环中从数组中删除项时遇到问题 var div=document.querySelector('div'); 变量数组=[ “iPad”, “iPod”, “iPhone” ]; for(设i=0;i

因此,我在for循环中从数组中删除项时遇到问题

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