css - 重寫checkbox樣式 用到id 與label關(guān)聯(lián)實(shí)現(xiàn) 但是現(xiàn)在checkbox循環(huán)生成多項(xiàng) 就不能用id了 求解決辦法
問題描述
<p class='checkbox'><input type='checkbox'><label for='awesome'></label> </p> <style>.checkbox input[type='checkbox']+label::before { content: ’a0’; display: inline-block; margin-right: 0em; border-radius: 1em; width: 1.3em; height: 1.3em; line-height: 1.1em; background: silver; text-indent: .2em; vertical-align: 0.2em; font-size: 20px;}.checkbox input[type='checkbox']:checked+label::before { background: yellowgreen; content: ’2605’;}input[type='checkbox'] { position: absolute; display: none;} </style>

現(xiàn)在頁面中有多個(gè)checkbox循環(huán)生成 就不能用id于label產(chǎn)生關(guān)聯(lián)了 請問怎么解決
問題解答
回答1:可以用 js 生成動態(tài) id 吧。在循環(huán)中給每個(gè) input 生成 id = 'awesome'+i,同時(shí)把 label 的 for 屬性值也設(shè)置成一樣的。下面是簡單的示例:
var body=document.getElementsByTagName(’body’)[0];for(var i=0;i<3;i++){ var input=document.createElement('input'); input.id='input'+i; var label=document.createElement('label'); label.setAttribute('for','input'+i); label.innerHTML='點(diǎn)擊'; body.appendChild(label) body.appendChild(input)}回答2:
<label class='label-checkbox'><input type='radio' name='my-radio' checked='checked'></label>
這么寫不就好了 多簡單
相關(guān)文章:
1. boot2docker無法啟動2. 關(guān)docker hub上有些鏡像的tag被標(biāo)記““This image has vulnerabilities””3. docker-compose中volumes的問題4. docker安裝后出現(xiàn)Cannot connect to the Docker daemon.5. java - SSH框架中寫分頁時(shí)service層中不能注入分頁類6. nignx - docker內(nèi)nginx 80端口被占用7. docker images顯示的鏡像過多,狗眼被亮瞎了,怎么辦?8. dockerfile - 為什么docker容器啟動不了?9. docker容器呢SSH為什么連不通呢?10. 關(guān)于docker下的nginx壓力測試

網(wǎng)公網(wǎng)安備