如何针对“换入场输入:检查”用JavaScript对CSS属性进行更改
JavaScript如何更改输入的CSS属性:检查
css
.switch-field input:checked + label {
background-color: #018786;
color:white;
box-shadow: none;
}
我想说我想更改其背景颜色 -
js
document.getElementsByClassName("switch-field input:checked")[0].style.background= #4400ff;
我如何才能定位该特定的特定特定”类“输入检查”?
How can Javascript change CSS properties of input:checked
CSS
.switch-field input:checked + label {
background-color: #018786;
color:white;
box-shadow: none;
}
Lets say I want to change its background color-
JS
document.getElementsByClassName("switch-field input:checked")[0].style.background= #4400ff;
How can i target that specific "class" with "input checked" ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
说明
基本上您需要遵循的步骤
输入:检查 +标签
(那是具有背景颜色属性的元素)gotcha意识到
onChange
事件,以确保其在检查输入建议
输入:检查 + Label
和input + input + label
classe要添加您的背景,justnument
,如果可以避免解决方案
,通常被认为是不良练习
我已经评论了JavaScript代码,如果您需要更多澄清,请随时评论此答案
Explanation
Basically the steps you need to follow are
input:checked + label
(thats the element which has the background color property)Gotcha to be aware of
input
'sonChange
event to ensure it sets the backgrounds properly on checking an unchecking the inputSuggestions
input:checked + label
andinput + label
classes to add your backgroundsconvention
and usually considered as bad practice if it can be avoidedSolution
I have commented the javascript code, if you need more clarification, feel free to comment on this answer
您可以将
QuerySelector
用于单个元素或QuerySelectorAll
用于多个元素。您可以在其中使用CSS查询。You can use
querySelector
for single element orquerySelectorAll
for multiple elements. You can use CSS query in that.