css - 重寫(xiě)checkbox樣式 用到id 與label關(guān)聯(lián)實(shí)現(xiàn) 但是現(xiàn)在checkbox循環(huán)生成多項(xiàng) 就不能用id了 求解決辦法
問(wèn)題描述
<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)在頁(yè)面中有多個(gè)checkbox循環(huán)生成 就不能用id于label產(chǎn)生關(guān)聯(lián)了 請(qǐng)問(wèn)怎么解決
問(wèn)題解答
回答1:可以用 js 生成動(dòng)態(tài) id 吧。在循環(huán)中給每個(gè) input 生成 id = 'awesome'+i,同時(shí)把 label 的 for 屬性值也設(shè)置成一樣的。下面是簡(jiǎn)單的示例:
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>
這么寫(xiě)不就好了 多簡(jiǎn)單
相關(guān)文章:
1. Docker for Mac 創(chuàng)建的dnsmasq容器連不上/不工作的問(wèn)題2. javascript - QWebEngineView 如何爬 angular 的動(dòng)態(tài)數(shù)據(jù)?3. javascript - 使用angular 的ui-sref 中出現(xiàn)了中文參數(shù),點(diǎn)擊跳轉(zhuǎn)后瀏覽器的地址欄里出現(xiàn)轉(zhuǎn)義后的%AE....%a%44. 工作近5年,3年Java Web ,近2年前端,未來(lái)何去何從?5. java - 郵箱如何發(fā)送html內(nèi)容6. html5 - 這個(gè)代碼顯示功能如何實(shí)現(xiàn)?7. javascript - 用JS 七牛上傳圖片出現(xiàn)文件已存在的錯(cuò)誤(file exists)8. java - 字節(jié)流轉(zhuǎn)成字符串之后,在通過(guò)字符串轉(zhuǎn)成字節(jié)流后的文件為什么會(huì)不一樣?9. css3 - 圖片等比例縮放10. java - 如何理解“不要通過(guò)共享內(nèi)存來(lái)通信,而應(yīng)該通過(guò)通信來(lái)共享內(nèi)存”?
