简体   繁体   English

使用 querySelector 将 For 和 id 添加到元素

[英]Adding the For and id to the element using querySelector

I have this fiddle我有这个小提琴

https://jsfiddle.net/z40j1gtp/1/ https://jsfiddle.net/z40j1gtp/1/

where i am trying to add the for to the parent node and then id to the checkbox so i can use them for accessibility, here ismy code我试图将 for 添加到父节点,然后将 id 添加到复选框,以便我可以将它们用于可访问性,这里是我的代码

<div class="cell">
  <span class="radio">
    <label role="radio" tabindex="0" class="el-radio" data-id="101148" aria-label="A_101148">
      <span class="el-radio__input">
        <span class="el-radio__inner"></span>
        <input type="radio" aria-hidden="true" tabindex="-1" class="el-radio__original" value="2">
      </span>
      <span class="el-radio__label">2</span>
    </label>
  </span>
</div>

my JS Code我的 JS 代码

document.querySelectorAll('.el-input__original').forEach((element, index) => {
      
var id = element.parentNode.parentNode.getAttribute('data-id')
  // element.setAttribute('id','A_'+id)
})

but its not adding it, i even tried adding the console in fiddle but its not showing up但它没有添加它,我什至尝试在小提琴中添加控制台但它没有出现

please first try to check if your selector is valid.请先尝试检查您的选择器是否有效。

document.querySelectorAll('.el-input__original')

will return an empty NodeList as the element does not exist.将返回一个空的 NodeList,因为该元素不存在。

i think you are trying to access "el-radio__original" class instead of el-input我认为您正在尝试访问“el-radio__original”class 而不是 el-input

please try请试试

document.querySelectorAll('.el-radio__original')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM