1. 程式人生 > >Html中使用自定義圖片來實現checkbox顯示

Html中使用自定義圖片來實現checkbox顯示

如果需要使用圖片來實現checkbox的使用,可以使用來實現,實現原理是將label表籤代替checkbox的顯示,將checkbox的display設定為none,在label標籤中使用要顯示的圖片img,再使用js函式去控制圖片的選中與否的切換。

    <label  for="agree" >           
        <img  class="checkbox" alt="選中" src="../img/checked.png" id="checkimg" onclick="checkclick();">
     </label>   
     <input
type="checkbox" style="display:none" id="agree" checked="checked">
<script> function checkclick(){ var checkimg = document.getElementById("checkimg"); if($("#agree").is(':checked') ){ $("#agree").attr("checked","unchecked"); checkimg.src="../img/unchecked.png"
; checkimg.alt="未選"; } else{ $("#agree").attr("checked","checked"); checkimg.src="../img/checked.png"; checkimg.alt="選中"; } }
</script>