单击时交换一组元素的 CSS 样式
我找到了一个线程,用 JavaScript 更改元素的类,即沿着我想要的路线,但我不知道如何实现它。
我有一个包含 4 个输入按钮和两种 CSS 样式的页面:“Selected”和“notSelected”。一个按钮最初将被硬编码为“选定”。当用户单击另一个按钮时,我想以编程方式迭代所有按钮(每页上的按钮数量将在 2 到 10 之间),将单击的按钮的类设置为“Selected”,并确保所有其他按钮按钮设置为“notSelected”。
我已经掌握了逻辑,但我以前从未使用过 JavaScript 做过任何事情,所以我对如何做到这一点一无所知。如果有人知道已经存在的教程/代码可以做到这一点,请为我指出正确的方向。
非常感谢!
I found a thread, Change an element's class with JavaScript, that is along the lines of what I'm going for, but I don't know how to implement it.
I have a page with 4 input buttons and two CSS styles: "Selected" and "notSelected". One button will be hard coded initially as "Selected". When the user clicks another button, I'd like to programatically iterate through all the buttons (the number of buttons on each page will be between 2 and 10), set the clicked button's class to "Selected", and make sure all the other buttons are set to "notSelected".
I've got the logic down, but I've never done anything with JavaScript before, so I haven't the slightest idea about how to do this. If someone knows of a tutorial/piece of code already out there that does this, please point me in the right direction.
Thanks a ton!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以采用简单的方法并使用像 jQuery 这样的框架来为您完成艰苦的工作
You can go the easy way and use a framework like jQuery that does the hard work for you
由于您是 JavaScript 新手,这可能有点多,但是您考虑过使用 jquery 吗?看一下toggleClass(): http://api.jquery.com/toggleClass/
As you are new to JavaScript, this might be a bit much, but have you considered using jquery? Take a look at toggleClass(): http://api.jquery.com/toggleClass/
您好,刚刚制作了一个快速脚本,希望对您有所帮助。如果您发现脚本有任何问题,请告诉我。
我使用的是焦点事件和输入框,您可以根据需要更改它。
div 标签内有一个表单标签 id="formDIV"
少量文本类型的输入标签和 onfocus="doSelect(this)"
这应该
帮助。
Hi just made a quick script, Hope that helps you. Let me know if you find any problem with the script.
I am using focus event and input box, you may change it as needed.
Have a form tag within the div tag id="formDIV"
Have few input tags of type text and onfocus="doSelect(this)"
this should
help.