2017-03-23 35 views
1

我試圖在angular2 + google材質上自定義複選框。 我創建了一個自定義CSS來替換複選框圖標,但我無法點擊它。更改複選框圖標不起作用

input[type=checkbox]{ 
    display: none; 
} 

input[type=checkbox]:checked + label:before{ 
    font-family: "Material Icons"; 
    content: '\E834'; 
    color: #295da7; 
    display: inline-block; 
    vertical-align: middle; 
} 

input[type=checkbox] + label:before{ 
    font-family: "Material Icons"; 
    content: '\E835'; 
    color: #295da7; 
    display: inline-block; 
    vertical-align: top; 
    text-align: center; 
    font-size: 18px; 
} 

style

,我可以點擊,瀏覽器渲染2個複選框的其他情形。

input[type=checkbox]:checked:before{ 
    font-family: "Material Icons"; 
    content: '\E834'; 
    color: #295da7; 
    display: inline-block; 
    vertical-align: middle; 
} 

input[type=checkbox]:before{ 
    font-family: "Material Icons"; 
    content: '\E835'; 
    color: #295da7; 
    display: inline-block; 
    vertical-align: top; 
    text-align: center; 
    font-size: 18px; 
} 

error2

我使用谷歌瀏覽器,但是,錯誤發生在其他瀏覽器。

+1

可以包括創建一個[MCVE]所需的代碼的其他人呢? –

+0

這是html代碼:' ' –

+0

@IgorNunes我認爲我發現問題...在根CSS和絕對定位。我已經更新了小提琴和片段。請再看一下。謝謝 –

回答

2

該複選框確實有效(某種方式),但您必須單擊「選中」一詞才能切換圖標。我做了一個片段來說明我的意思。我還沒有找到解決方法,但如果以後找到一個,我會發佈一個。

編輯:好吧,這是比你的原始代碼有點不同,但它的作品!

希望這有助於:)

@font-face { 
 
    font-family: "MaterialIcons"; 
 
    src: url(https://github.com/google/material-design-icons/blob/master/iconfont/MaterialIcons-Regular.ttf); 
 
} 
 

 
[name=check]{ 
 
    display: none!important; 
 
} 
 

 
[for^=check]{ 
 
    font-family: "MaterialIcons"; 
 
    content: '\E834'; 
 
    position: relative; 
 
    margin:38px 
 
} 
 

 
[for^=check]:before{ 
 
    font-family: "MaterialIcons"; 
 
    content: '\E834'; 
 
    position: relative; 
 
    padding: 5px; 
 
    width: 45px; 
 
    margin-right:10px; 
 
    height: 25px; 
 
    background: white; 
 
} 
 

 
[type=checkbox]:checked + [for^=check]:before{ 
 
    background: white; 
 
    font-family: "MaterialIcons"; 
 
    content: '\E835'; 
 
    width:90px; 
 
} 
 
[for^=check], input[name=check]{display:inline-block; vertical-align:top; position:relative;}
<input id=check-1 type=checkbox name=check /> 
 
<label for=check-1>Checked</label> 
 
<input id=check-2 type=checkbox name=check /> 
 
<label for=check-2>Unchecked</label>

+0

順便說一句,我做了一個小提琴https://jsfiddle.net/x342atcq/ –

+0

感謝上帝的舊小提琴 - 發現這個http://jsfiddle.net/RachGal/cvfh5oqd/,我最初爲單選按鈕,但爲複選框工作。值得一看! –

+0

ta-da! http://jsfiddle.net/RachGal/um37yaz3/ –