Home > php教程 > PHP源码 > Html中使用自定义图片来实现checkbox显示的方法_HTML/Xhtml_网页制作

Html中使用自定义图片来实现checkbox显示的方法_HTML/Xhtml_网页制作

PHP中文网
Release: 2016-05-25 17:10:25
Original
1394 people have browsed it

下面小编就为大家带来一篇Html中使用自定义图片来实现checkbox显示的方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

如果需要使用图片来实现checkbox的使用,可以使用来实现,实现原理是将label表签代替checkbox的显示,将checkbox的display设置为none,在label标签中使用要显示的图片img,再使用js函数去控制图片的选中与否的切换。


JavaScript Code复制内容到剪贴板

  1.         class="checkbox" alt="选中" src="../img/checked.png" id="checkimg" onclick="checkclick();">   

  2.            

  3.      "checkbox" style="display:none" id="agree" checked="checked">   

  4.      <script>   </p></li> <li><p>         <span class="keyword">function</span> checkclick(){   </p></li> <li><p>            <span class="keyword">var</span> checkimg = document.getElementById(<span class="string">"checkimg"</span>);   </p></li> <li><p>            <span class="keyword">if</span>($(<span class="string">"#agree"</span>).is(<span class="string">':checked'</span>) ){   </p></li> <li><p>                $(<span class="string">"#agree"</span>).attr(<span class="string">"checked"</span>,<span class="string">"unchecked"</span>);   </p></li> <li><p>                checkimg.src=<span class="string">"../img/unchecked.png"</span>;   </p></li> <li><p>                checkimg.alt=<span class="string">"未选"</span>;   </p></li> <li><p>            } <span class="keyword">else</span>{   </p></li> <li><p>                $(<span class="string">"#agree"</span>).attr(<span class="string">"checked"</span>,<span class="string">"checked"</span>);   </p></li> <li><p>                checkimg.src=<span class="string">"../img/checked.png"</span>;   </p></li> <li><p>                checkimg.alt=<span class="string">"选中"</span>;   </p></li> <li><p>            }   </p></li> <li><p>        }   </p></li> <li><p>    </script>  

以上就是小编为大家带来的Html中使用自定义图片来实现checkbox显示的方法全部内容了,希望大家多多支持脚本之家~

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template