如何针对“换入场输入:检查”用JavaScript对CSS属性进行更改

发布于 2025-02-12 03:07:18 字数 417 浏览 2 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一场信仰旅途 2025-02-19 03:07:18

说明

基本上您需要遵循的步骤

  1. 清楚所有标签背景样式属性
  2. 选择所有输入:检查 +标签(那是具有背景颜色属性的元素)
  3. 循环通过所有获取的标签
  4. ,背景颜色是您想要的

gotcha意识到

  1. 通过JavaScript更改背景属性的内容,不会在未选中输入时自动更改它
  2. onChange事件,以确保其在检查输入

建议

  1. 时正确设置背景,请使用CSS 输入:检查 + Labelinput + input + label classe要添加您的背景,
  2. 请始终尝试避免使用JavaScript操纵样式,它违反justnument,如果可以避免

解决方案

,通常被认为是不良练习

function clickHandle() {
  // Clear all the labels background style property to ensure it resets to the css class background property
  clearLabelBackground();

  // Get all the labels (not input) based on the css input:checked flag
  // We are getting the label because that if the element which has the background color property you want to change
  var selectedLabels = document.querySelectorAll("input:checked + label");
  
  // Loop through each of the fetched labels
  selectedLabels.forEach((label)=>{
    
    // Change the background color style
    label.style.background="#4400ff"; 

  });
}

function clearLabelBackground() {
  // Get all the labels (not input) regardless of the selection
  // We are getting the label because that if the element which has the background color property you want to change
  var selectedLabels = document.querySelectorAll("input + label");
  
  // Loop through each of the fetched labels
  selectedLabels.forEach((label)=>{
    
    // Remove the background color style
    label.style.background=""; 

  });

}
.switch-field input:checked + label {
    background-color: #018786;
    color:white;
    box-shadow: none;
}
<div class="switch-field">
  <input type="checkbox"/>
  <label>Item 1</label>
</div>
<div class="switch-field">
  <input type="checkbox"/>
  <label>Item 2</label>
</div>
<div class="switch-field">
  <input type="checkbox"/>
  <label>Item 3</label>
</div>
<div class="switch-field">
  <input type="checkbox"/>
  <label>Item 4</label>
</div>

<button onclick="clickHandle()">Change BG</button>

我已经评论了JavaScript代码,如果您需要更多澄清,请随时评论此答案

Explanation

Basically the steps you need to follow are

  1. Clear all the label background style property
  2. Select all the input:checked + label (thats the element which has the background color property)
  3. Loop through all the fetched labels
  4. Change the background color to what you want

Gotcha to be aware of

  1. Changing the background property via javascript will not change it back automatically when the input is unchecked
  2. You will need to run a clear/update function on the input's onChange event to ensure it sets the backgrounds properly on checking an unchecking the input

Suggestions

  1. Use the CSS input:checked + label and input + label classes to add your backgrounds
  2. Always try to avoid javascript manipulation of styles, it's against the convention and usually considered as bad practice if it can be avoided

Solution

function clickHandle() {
  // Clear all the labels background style property to ensure it resets to the css class background property
  clearLabelBackground();

  // Get all the labels (not input) based on the css input:checked flag
  // We are getting the label because that if the element which has the background color property you want to change
  var selectedLabels = document.querySelectorAll("input:checked + label");
  
  // Loop through each of the fetched labels
  selectedLabels.forEach((label)=>{
    
    // Change the background color style
    label.style.background="#4400ff"; 

  });
}

function clearLabelBackground() {
  // Get all the labels (not input) regardless of the selection
  // We are getting the label because that if the element which has the background color property you want to change
  var selectedLabels = document.querySelectorAll("input + label");
  
  // Loop through each of the fetched labels
  selectedLabels.forEach((label)=>{
    
    // Remove the background color style
    label.style.background=""; 

  });

}
.switch-field input:checked + label {
    background-color: #018786;
    color:white;
    box-shadow: none;
}
<div class="switch-field">
  <input type="checkbox"/>
  <label>Item 1</label>
</div>
<div class="switch-field">
  <input type="checkbox"/>
  <label>Item 2</label>
</div>
<div class="switch-field">
  <input type="checkbox"/>
  <label>Item 3</label>
</div>
<div class="switch-field">
  <input type="checkbox"/>
  <label>Item 4</label>
</div>

<button onclick="clickHandle()">Change BG</button>

I have commented the javascript code, if you need more clarification, feel free to comment on this answer

微凉 2025-02-19 03:07:18

您可以将QuerySelector用于单个元素或QuerySelectorAll用于多个元素。您可以在其中使用CSS查询。

document.querySelector("switch-field input:checked").style.background = 
 "#4400ff";

You can use querySelector for single element or querySelectorAll for multiple elements. You can use CSS query in that.

document.querySelector("switch-field input:checked").style.background = 
 "#4400ff";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文