一般的input框比較簡單,我們可以用JavaScript配合css背景圖片定位讓我們模擬寫出一個點擊選中效果
首先需要有個圖片素材,當頁面加載的時候是背景圖片定位到左圖,當我們點擊圖片的時候,背景圖片定位到右圖
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <script type="text/javascript" src="js/jquery.js"></script> <style type="text/css"> .check_box{ height: 20px; width: 20px; display: block; background: url("images/shop-icon.png") no-repeat; background-size: 50px 100px; } /*屬性選擇器*/ .check_box[checked]{ background-position: -25px 0; } </style> </head> <body> <span class="check_box"></span> <script type="text/javascript"> var checkBtn = document.getElementsByClassName('check_box'); for(var j = 0 ; j < checkBtn.length; j++){ checkBtn[j].onclick = function(){ var hasChecked = this.getAttribute('checked'); if(hasChecked !== null){ this.removeAttribute('checked'); }else{ this.setAttribute('checked',' '); } } } </script> </body> </html>