【问题标题】:Display labels with "selected" indicator显示带有“选定”指示器的标签
【发布时间】:2014-06-10 11:36:49
【问题描述】:

我在 durandal 项目中工作,并且有带有 3 个标签的 html 页面。 我希望标签的行为类似于单选按钮:当一个被选中时 - 另一个未被选中。

我希望标签是灰色的,而选中的标签(用户通过点击选择)会变成黑色。

当然,我可以用jquery来做,比如:

html:

 <label id="large" data-bind="click:change" style="color:Black">
 <label id="medium" data-bind="click:change" style="color:Gray">
 <label id="small" data-bind="click:change" style="color:Gray">

java脚本:

 var selectedId = "large"; //global variable
 function change(e, s){
   $('#'+selectedId).css('color', 'Gray'); // change previous-selection to gray
   $('#'+sender.currentTarget.id).css('color', 'Black');// change current-selection to gray
   selectedId = sender.currentTarget.id;
 }

但是,有没有更好的方法呢?

【问题讨论】:

  • 你的问题是什么?
  • 有人编辑了我的帖子,最后删除了这个问题。我退了;立即阅读

标签: javascript jquery html css durandal


【解决方案1】:

给它们一个通用的类,比如“.labels”,把它们包装起来,html就变成了:

 <div class="label-container">
    <label class="labels" id="large">large</label>
    <label class="labels" id="medium">medium</label>
    <label class="labels" id="small">small</label>
</div>

在您的 css 中定义 2 个类,一个具有活动状态(黑色):

.labels{
color:#ededed;
}

.active{
color:#000;
}

还有你的 javascript/jquery:

$(document).on('click', '.labels', function(){
  $(this).addClass('active').siblings().removeClass('active');
});

你就完成了。 Fiddle 有关纯 css 解决方案,请参阅@gulty 的答案。

【讨论】:

    【解决方案2】:

    使用纯 CSS 和隐藏单选按钮: http://jsfiddle.net/GFC2G/2/

    input[type="radio"]{display:none}
    input[type="radio"]+label{ 
        color:red;
       -webkit-transition:0.2s all linear;
       -moz-transition:0.2s all linear;
       -o-transition:0.2s all linear;
       transition:0.2s all linear;
    }
    input[type="radio"]:checked+label{ 
        color:black; 
    }
    

    HTML:

    <input id="radio1" type="radio" name="rad"><label for="rad">Radio 1</label>
    <input id="radio2" type="radio" name="rad"><label for="rad">Radio 2</label>
    

    【讨论】:

    • 我喜欢你的回答,但操作似乎并没有使用实际的复选框(不是那个不赞成的)
    • 他要求提供 css 解决方案,“当然,我可以通过 jquery 来完成,例如:”,因为他没有提供有关他使用的标签类型的更多信息..
    • @gulty 他不想输入,但是labels to behave like radio-buttons
    • 我认为人们实际上应该能够思考。我给了一个 JS 免费提示 - 现在我更新了隐藏单选按钮的帖子。不会改变这就像一个魅力的事实。
    • 为了避免更多的反对票(这是 IMO 的最佳答案),请在帖子顶部声明:“您不想要单选按钮?这只是外观吗?我们可以将它们隐藏到得到你想要的结果”。或者随便什么:)
    【解决方案3】:

    工作代码如下,这将 100% 工作 http://jsfiddle.net/bqLbu/1/

        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
        </script>
    
    
        <script>
    
    $(function () { 
    $('label').click(function () { //when you will click on any label
    var selectedId=$(this).attr('id');//get id of that label
    
       $('#'+selectedId).css('color', 'black'); // change selection to black
    
        $('label').not('#'+selectedId).css('color', 'grey');  //others to grey
     }); 
      });
    
     </script>
    
    
    <label id="large" data-bind="click:change" style="color:green">hi22</label>
     <label id="medium" data-bind="click:change" style="color:green">hi 2342</label>
     <label id="small" data-bind="click:change" style="color:green">hi424</label>
    

    【讨论】:

    • 1.事件委托。 2. 不需要包装这个你可以用vanilla js(this.id)获取id属性。 3.无需通过js应用css。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-19
    • 1970-01-01
    • 2018-12-18
    • 2015-06-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多