使用DOM操作选择列表中的项目

问题描述 投票:0回答:1
document.getElementsByClassName('input-wrapper')

结果

The result

有没有办法通过执行dom操作来选择离子列表的第二项?

尝试使用选定的索引不起作用

javascript angular typescript ionic-framework
1个回答
0
投票

使用可以选择带索引的第二项,检查片段

var el = document.getElementsByClassName('input-wrapper');
Object.keys(el).forEach(item=>{
 el[item].addEventListener('click' ,()=>{
  el[item].children[0].checked = !el[item].children[0].checked
  console.log(el[item]);  
})
})
<div class="input-wrapper">1 label <input type="radio" value="select"> Label</div>
<div class="input-wrapper">2 label <input type="radio" value="select"> Label</div>
<div class="input-wrapper">3 label
  <input type="radio" value="select"> Label
</div>
<div class="input-wrapper">4 label <input type="radio" value="select"> Label </div>
<div class="input-wrapper">5 label <input type="radio" value="select"> Label </div>
<div class="input-wrapper">6 label <input type="radio" value="select"> Label </div>
© www.soinside.com 2019 - 2024. All rights reserved.