简体   繁体   English

Javascript:如何在 jss 中使用相邻兄弟选择器<input type="radio"> // 点击时改变颜色

[英]Javascript: How to use adjacent sibling selector in jss with <input type=radio> // change color when clicked

I am trying to change the color of the label on a radio button in jss when it is clicked.我试图在单击 jss 中的单选按钮时更改 label 的颜色。 This is what I have so far in Form.jsx:这是我目前在 Form.jsx 中的内容:

<section className={classes.buttons} data-toggle="buttons">
  <label className={classes.btn}>
    <input type="radio" name="options" id="option1" checked/>
    <span>9th grade</span>
  </label>
  <label className={classes.btn}>
    <input type="radio" name="options" id="option2"/> <span>10th grade</span>
  </label>
  <label className={classes.btn}>
    <input type="radio" name="options" id="option3"/> <span>11th grade</span>
  </label>
  <label className={classes.btn}>
    <input type="radio" name="options" id="option4"/> <span>12th grade</span>
  </label>
</section>

In my formStyles.js file,在我的 formStyles.js 文件中,

    buttons : {
        display: 'grid',
        gridGap: '50px',
        gridTemplateColumns: 'auto auto',
        background: 'rgba(255 , 255, 255, 0.25)',
        padding: '30px',
        margin: '30px 100px 10px 100px', 
        zIndex: '1',
        
        
        "& input[type = radio]": {
            opacity: '0',
            position: 'fixed',
            width: '0',
        
        "&:checked &span"  : {
            display: 'none',
            color: 'blue',
            fontSize: '30px',
            
           },
        },
        "& label": {
            backgroundColor: '#BC98EE',
            zIndex: '2',
            color: 'white',

        },
    },

It works only when I do "&:checked" but not when I add "&span".它仅在我执行“&:checked”时有效,但在添加“&span”时无效。 In fact, it does not run anything under "&:checked &span" when I add "&span".事实上,当我添加“&span”时,它不会在“&:checked &span”下运行任何东西。 Any suggestions?有什么建议么?

The span is a sibling of the checked input, not a descendant.跨度是检查输入的兄弟,而不是后代。

See: Adjacent Sibling Combinator请参阅: 相邻兄弟组合器

 .buttons { padding: 30px; }.buttons input:checked+span { color: blue; }
 <section class="buttons"> <label> <input type="radio" name="options" id="option1" checked /> <span>9th grade</span> </label> <label> <input type="radio" name="options" id="option2"> <span>10th grade</span> </label> <label> <input type="radio" name="options" id="option3"/> <span>11th grade</span> </label> <label> <input type="radio" name="options" id="option4" /> <span>12th grade</span> </label> </section>

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

相关问题 2 类型:单击时改变颜色的单选按钮 React - 2 type: radio buttons that change color when clicked React 如何在 Angular 中使用 CSS Adjacent 兄弟选择器? - How do I use the CSS Adjacent sibling selector in Angular? jss如何改变颜色的不透明度 - jss how to change opacity for a color Javascript 单击时如何更改按钮的颜色? - Javascript how to change the color of a button when clicked? 选择输入(单选按钮)时如何使用javascript / jquery更改按钮的颜色 - How to change color of button with javascript/jquery when input (radio buttons) is selected 单击时如何使用javascript更改单选按钮的样式 - How to change the style of a radio button with javascript when clicked 如何在使用 JavaScript 选择单选输入时更改 div 的颜色 - How to change the color of div while a radio input is selected using JavaScript 使用javascript单击时获取单选输入的值 - Get the value of a radio input when clicked with javascript 如何使用颜色类型的输入元素与 javascript 更改出现在可编辑 iframe 中的字体颜色 - How to use the input element of type color with javascript to change the font color appearing in an editable iframe 如何更改文本的颜色,并在javascript中单击4次按钮时停止 - How to change color of text and stop when clicked the button with 4 times in javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM