首页 > Web开发 > 详细

用js写出一个漂亮的单选框选中效果

时间:2020-05-01 20:49:59      阅读:79      评论:0      收藏:0      [点我收藏+]

一般的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>

 

用js写出一个漂亮的单选框选中效果

原文:https://www.cnblogs.com/shangrao/p/12814441.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!