简体   繁体   English

通过使用CSS / HTML / JavaScript选择A或C来更改B的状态

[英]Changing the state of B by selecting A or C with CSS/HTML/JavaScript

I've a 3 way html/CSS/JavaScript toggle (radio buttons) set up here . 我有一个3路的HTML / CSS / JavaScript的设置切换(单选按钮) 在这里

3 options: A / "X" / B Where "X" is none. 3个选项: A / "X" / B其中“ X”不存在。

HTML HTML

<div class="ABSelector">  
    <label class="Astate">
    A
    <input name="state" type="radio" value="A" />
    </label>

    <label class="nostate">
    x
    <input name="state" type="radio" value="" checked />
    </label>

    <label class="Bstate">
    B
    <input name="state" type="radio" value="B" />
    </label>

</div>

Relevant bit of the CSS CSS的相关位

label {
    cursor: pointer;
    display: block;
    float: left;
    width:40px;
    height:40px;
    //background-color:green;
    line-height:40px;
    text-align:center;

}
label.Astate.selected {
    background-color:indianred;
    color: white;
    border-top-left-radius:10px;  
    border-bottom-left-radius:10px;
}
label.Astate.selected .nostate{
    color:grey;
}
label.Bstate.selected {
    background-color:orange;
    color: white;
    border-top-right-radius:10px;  
    border-bottom-right-radius:10px;
}
label.nostate.selected {
    background-color:white;
    color: white;
}

Javascript 使用Javascript

window.addEvent('domready', function() {
  $$('input').set({
    events: {
      change: function(el) {
        $$('label').removeClass('selected');
        this.getParent('label').addClass('selected');
      }
    }
  });
});

The JavaScript enables/disables .selected JavaScript启用/禁用.selected

When the page loads initially the X is visible. 最初加载页面时,X可见。 Only after the first clicks the X will then only appears if A or B has been clicked. 只有在第一次单击后,X才会出现,如果已单击A或B。 If the X is clicked it disappears. 如果单击X,它将消失。

I'd like to set it so the X only appears when either A or B is clicked. 我想设置它,以便X仅在单击A或B时出现。 even when the page loads. 即使页面加载。

Am I missing something on the CSS side or does this need to be solved with JavaScript? 我是否在CSS方面缺少某些东西,还是需要使用JavaScript解决?

You can just amend your html with this, adding the selected class to your label, making it in fact, invisible: 您可以使用此方法修改html,将选定的类添加到标签中,使其实际上不可见:

<label class="nostate selected">
x
<input name="state" type="radio" value="" checked />
</label>

It is happening because of 这是因为

    label.nostate.selected {
    background-color: white;
    color: white;

it sets color of text to white which is not visible as background in white. 它将文本的颜色设置为白色,而白色则看不到背景。 Change color of text or its background 更改文字或背景的颜色

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

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