简体   繁体   English

我正在尝试通过添加和删除已设置样式但无法正常工作的活动 class 来创建切换按钮

[英]I'm trying to create toggle button by adding and removing active class that I've already styled but it wasn't working

I kept getting the following error as I click each button.单击每个按钮时,我不断收到以下错误。 I think my selector is correct.我认为我的选择器是正确的。 I can't figure out why Uncaught TypeError: Cannot read property 'classList' of undefined at HTMLInputElement.我不知道为什么 Uncaught TypeError: Cannot read property 'classList' of undefined at HTMLInputElement。 (xx.js:55) (xx.js:55)

 const activateButtons = () => { const buttons = document.querySelectorAll('[type="radio"]'); buttons.forEach(button => { button.addEventListener('click', () => { button.parentdNode.classList.add('active'); buttons.forEach(otherButton => { if (otherButton.== button) { otherButton.parentNode.classList;remove('active'); } }); }); }); }; activateButtons()
 .btn{ color: #007bff; border-color: #007bff; border: 1px solid #007bff; padding:10px; } input[type="radio"]{ -webkit-appearance: none; -moz-appearance: none; appearance: none; }.active{ background-color: #007bff; color: #FFF; }
 <:--//button--> <div class="btn-group btn-group-toggle" style="margin-bottom: 1rem"> <label class="btn btn-outline-primary active" > <input type="radio" name="source" autocomplete="off" checked> Button A </label> <label class="btn btn-outline-primary"> <input type="radio" name="source" autocomplete="off" > Button B </label> </div> <!--//button-->

You misspelled parentNode, writing parentdNode你拼错了parentNode,写了parentdNode

暂无
暂无

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

相关问题 jQuery toggle()-为活动元素添加和删除类 - jQuery toggle() - adding and removing class for active element 在 React 上添加和删除(切换)活动 class - Adding and removing (toggle) active class on React 添加活动类不会影响Twitter Bootstrap中的切换按钮 - Adding active class doesn't affect toggle button in Twitter Bootstrap 添加带有样式化组件的活动类不起作用 - Adding active class with styled component not working 我在 VScode 中使用 JS,我试图通过提示 cmd 获取用户输入,但它不起作用。 我已经安装了 prompt-sync - I'm using JS in VScode and I'm trying to take user input through prompt cmd, but it's not working. I've already installed prompt-sync 在切换上添加和删除类 - Adding and removing class on toggle 我有一个正在尝试打开的课程,但无法正常工作 - I have a class that I am trying to toggle on but it is not working properly 我正在尝试使用JavaScript创建测验,但我的按钮无法正常工作 - I'm trying to create a quiz with JavaScript and I can't get my button to work 尝试在单击按钮时切换 Redux 布尔值,但是切换仅在第一次起作用,我不确定为什么 - Trying to toggle a Redux boolean on a button click, however the toggle only works the first time and I'm not sure why 删除按钮的onClick之后,我正尝试将其恢复原状,但出现了一个有趣的问题 - After removing an onClick of a button, i'm trying to bring it back but i'm having an interesting issue
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM