Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
431 views
in Technique[技术] by (71.8m points)

reactjs - 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. This is what I have so far in 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,

    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". In fact, it does not run anything under "&:checked &span" when I add "&span". Any suggestions?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...