简体   繁体   English

在事件侦听器中更改 CSS 复选框边框颜色

[英]Change CSS checkbox border color in event listener

These little code snippets resemble two custom CSS checkboxes, based on this article .这些小代码片段类似于基于 本文的两个自定义 CSS 复选框。

For your convenience: jsfiddle .为了您的方便: jsfiddle

I have two problems with this code:这段代码有两个问题:

  1. If clicking the 1st checkbox to unchecked, I want the event listener function to change the border's color of checkbox 1 to red.如果单击第一个复选框未选中,我希望事件侦听器 function 将复选框 1 的边框颜色更改为红色。 Methinks I'd have to change the object's :before properties?我认为我必须更改对象的:before属性? Anyway, I can't figure out how it's done and don't want to use jquery, font awesome or other external libraries, just pure CSS and js.无论如何,我想不通它是如何完成的,也不想使用 jquery、font awesome 或其他外部库,只是纯粹的 CSS 和 js。

  2. The 2nd checkbox is intentionally not working.第二个复选框故意不起作用。 In order to make it work, I'd have to embrace the <input> with the <label> statements like in checkbox 1. Why doesn't it work with <label for="..."> ?为了使它起作用,我必须将<input>与复选框 1 中的<label>语句结合起来。为什么它不能与<label for="...">一起使用? Actually I'd like to get rid of the labels alltogether.实际上,我想一起摆脱标签。

HTML: HTML:

 document.getElementById("first").addEventListener("click", () => { const e = event.srcElement; /* HOW CAN I CHANGE THE BORDER'S COLOR ONCLICK? */ console.log(e.style); if (e.checked) { e.style.bordercolor = 'gray'; } else { e.style.borderColor = 'red'; e.style.borderBlockColor = 'red'; e.style.accentColor = 'red'; e.style.borderAfterColor = 'red'; e.style.borderBeforeColor = 'red'; e.style.outlineColor = 'red'; e.style.overrideColors = 'red'; } });
 .cbx { display: none; /* hide the regular checkbox */ }.cbx+*::before { /* settings if checkbox is unchecked */ flex-shrink: 0; display: inline-block; vertical-align: bottom; width: 1rem; height: 1rem; margin-right: 0.3rem; border-radius: 10%; border-style: solid; border-width: 0.1rem; content: ""; /* no mark */ border-color: gray; /* border color DON'T CHANGE IT HERE TO RED. Initially it should be gray */ background: pink; /* background */ }.cbx+* { display: inline-flex; padding: 0.5rem 1rem; color: purple; /* label color if unchecked */ }.cbx:checked+* { color: red; /* label color if checked */ }.cbx:checked+*::before { /* settings if checked */ content: "X"; /* the check mark */ color: white; /* mark's color */ text-align: center; background: green; /* background if checked */ border-color: yellow; /* border if checked */ }
 <form> <label> <input id="first" class="cbx" type="checkbox" /> <span>First</span> </label> <.-- 2nd NOT WORKING IF <label> doesn't embrace <input> like in checkbox 1? WHY NOT? --> <label for="second">Second</label> <input id="second" class="cbx" type="checkbox" name="second" /> </form>

Add an extra CSS class for the red border to override.cbx + *::before为红色边框添加一个额外的 CSS class 以覆盖.cbx + *::before

.cbx.redBorder + *::before {
  border-color: red;
}

Add and remove this class to the checkbox onclick:将此 class 添加和删除到复选框 onclick 中:

document.getElementById("cb").addEventListener("click", () => {
    var e=event.srcElement;
    if (e.checked) {
        e.classList.remove("redBorder");
    } else {
        e.classList.add("redBorder");
    }
});

Toggle won't work because the checkbox starts unchecked and thus can't remove redBorder if clicked. Toggle 将不起作用,因为复选框开始未选中,因此如果单击则无法删除 redBorder。

https://jsfiddle.net/jamacoe/nL942u1h/119/ https://jsfiddle.net/jamacoe/nL942u1h/119/

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

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