twitter-bootstrap 是否可以将输入复选框设为 Bootstrap glyphicon?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28196795/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 22:14:43  来源:igfitidea点击:

Is it posible to make an input checkbox a Bootstrap glyphicon?

csstwitter-bootstrapcheckbox

提问by Dirk Jan

Is it posible to make an input checkbox a Bootstrap glyphicon?

是否可以将输入复选框设为 Bootstrap glyphicon?

I want to make up the default checkboxes with a nice Bootstrap glyphicon. For example: glyphicon glyphicon-uncheckedand when checked: glyphicon glyphicon-check.

我想用一个漂亮的 Bootstrap glyphicon 来组成默认复选框。例如:glyphicon glyphicon-unchecked检查时:glyphicon glyphicon-check

I've tried this:

我试过这个:

input[type='checkbox'] {
  position: relative;
  top: 1px;
  display: inline-block;
  font-family: 'Glyphicons Halflings';
  font-style: normal;
  font-weight: 400;
  line-height: 1;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  content: "\e157";
}

But nothing happened..

但是什么都没发生..

I don't know if that's posible?

不知道可不可以?

回答by RGLSV

You can achieve that in a couple of methods:

您可以通过以下几种方法来实现:

Method 1

方法一

  • inside a <label>tag, two <tags>that represent the icons that you want need to be placed(or outside, per use scenario)
  • then toggle these two <tags>, when the input[type='checkbox']is checked or unchecked
  • done.
  • <label>标签内部,两个<tags>代表您想要放置的图标(或外部,每个使用场景)
  • 然后<tags>input[type='checkbox']选中或未选中时切换这两个
  • 完毕。

Method 2

方法二

  • a cleaner approach to the above one, would be to use the css from bootstraps icons, and place them in a :before(or :afterdepending on your scenarion) on the <label>tag
  • then toggle the contentprop. of the :beforeclass, that the icons that you want have, when the input[type='checkbox']is checked or unchecked
  • done.
  • 对上述方法的一种更简洁的方法是使用引导程序图标中的 css,并将它们放在标签上:before(或:after取决于您的场景)<label>
  • 然后切换content道具。在:before课堂上,当input[type='checkbox']检查或未选中时,您想要的图标具有
  • 完毕。


Check out the demo hereand also, a couple of more through documentation on this matter:

这里查看演示,还有更多关于这个问题的文档:

回答by isherwood

If you're able to modify your markup a bit, this should do:

如果您能够稍微修改您的标记,则应该这样做:

<label for="myCheckbox" class="glyphy">
    <input type="checkbox" id="myCheckbox" /> 
    <span class="glyphicon glyphicon-unchecked"></span>
    label words
</label>

$('.glyphy').click(function (e) {
    if ($(e.target).is('input')) { // prevent double-event due to bubbling
        $(this).find('.glyphicon').toggleClass('glyphicon-check glyphicon-unchecked');
    }
});

Demo

演示

回答by indubitablee

if you have the icons, you can style it as such: http://jsfiddle.net/swm53ran/164/

如果你有图标,你可以这样设计:http: //jsfiddle.net/swm53ran/164/

/*hide checkbox and radio buttons*/
input[type=checkbox], 
input[type=radio] {
    width: 2em;
    margin: 0;
    padding: 0;
    font-size: 1em;
    opacity: 0; /*This is the part tht actually hides it*/
}

/*normalize the spacing*/
input[type=checkbox] + label,
input[type=radio] + label {
    display: inline-block;
    margin-left: -2em;
    line-height: 1.5em;
}

/*unchecked css*/
input[type=checkbox] + label > span,
input[type=radio] + label > span {
    display: inline-block;
    background-image: url('http://upload.wikimedia.org/wikipedia/commons/thumb/0/06/Face-sad.svg/48px-Face-sad.svg.png');
    width: 50px;
    height: 50px;
}

/*selected checkbox css*/
input[type=checkbox]:checked + label > span > span {
    width: 50px;
    height: 50px;
    display:block;
    background-image: url('http://wscont1.apps.microsoft.com/winstore/1x/a14c3995-34d7-454c-82e2-0c192e48b91a/Icon.173718.png');
}

/*selected radio css*/
input[type=radio]:checked + label > span > span {
    width: 50px;
    height: 50px;
    display:block;
    background-image: url('http://wscont1.apps.microsoft.com/winstore/1x/a14c3995-34d7-454c-82e2-0c192e48b91a/Icon.173718.png');
}

<div>
    <input id="check1" type="checkbox" name="check1" value="check1" />
    <label for="check1"><span><span></span></span>Checkbox</label>
</div>
<div>
    <input id="radio1" type="radio" name="radio" value="radio1" />
    <label for="radio1"><span><span></span></span>Radio1</label>
</div>
<div>
    <input id="radio2" type="radio" name="radio" value="radio2" />
    <label for="radio2"><span><span></span></span>Radio2</label>
</div>