博主信息
博文 5
粉丝 0
评论 0
访问量 4028
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
JS加入购物车(时间:2019.01.22)
Less的作业
原创
848人浏览过
实例
<!DOCTYPE html>
<html lang = "en">
<head>
    <meta charset = "UTF-8">
    <title>加入购物车</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        
        #shop {
            width: 400px;
            border: 1px solid #C40000;
            margin: 20px auto;
        }
        
        #tips {
            height: 35px;
            line-height: 35px;
            text-align: center;
            color: #FFF;
            background: #C40000;
        }
        
        .info {
            margin-top: 10px;
            overflow: hidden;
        }
        
        .title, .op {
            width: 90px;
            height: 26px;
            color: #838383;
            font-size: 12px;
            font-weight: bold;
            text-align: center;
            line-height: 26px;
            display: block;
            float: left;
            margin: 0 2px;
        }
        
        .op {
            font-weight: normal;
            border: 1px solid #CCC;
        }
        
        .op:hover, #btn:hover {
            cursor: pointer;
        }
        
        .select {
            width: 88px;
            height: 24px;
            line-height: 24px;
            color: #C40000;
            border: 2px solid #C40000;
        }
        
        #btn {
            width: 120px;
            height: 35px;
            color: #FFF;
            text-align: center;
            line-height: 35px;
            background: #C40000;
            margin: 20px 0 20px 96px;
        }
    </style>
</head>
<body>
<div id = "shop">
    <div id = "tips">请选择信息后加入购物车</div>
    <div id = "content">
        <div class = "info">
            <span class = "title">版本</span>
            <span class = "op">ONE A2001</span>
            <span class = "op">ONE A0001</span>
            <span class = "op">ONE A1001</span>
        </div>
        <div class = "info">
            <span class = "title">机身颜色</span>
            <span class = "op">白色</span>
            <span class = "op">黑色</span>
            <span class = "op">金色</span>
        </div>
        <div class = "info">
            <span class = "title">套餐类型</span>
            <span class = "op">标配</span>
            <span class = "op">套餐一</span>
            <span class = "op">套餐二</span>
        </div>
        <div class = "info">
            <span class = "title">运行内存</span>
            <span class = "op">2GB</span>
            <span class = "op">4GB</span>
            <span class = "op">8GB</span>
        </div>
        <div class = "info">
            <span class = "title">机身内存</span>
            <span class = "op">16GB</span>
            <span class = "op">32GB</span>
            <span class = "op">64GB</span>
        </div>
        <div class = "info">
            <span class = "title">产地</span>
            <span class = "op">中国大陆</span>
            <span class = "op">港澳台</span>
        </div>
        <div class = "info">
            <span class = "title">价格</span>
            <span class = "op">999元抢购</span>
        </div>
        <div class = "info">
            <span class = "title">数量</span>
            <input type = "number" class = "op" min = "1" max = "10">
        </div>
        <div id = "btn">加入购物车</div>
    </div>
</div>
</body>
<script src = "https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
<script>
    $(function () {
        $('span.op').click(function () {
            if ($(this).hasClass('select')) {
                $(this).removeClass('select');
            } else {
                $(this).addClass('select').siblings(this).removeClass('select');
            }
        });
        //将用户选择的信息放入一个数组中
        $('#btn').click(function () {
            var shop = [];
            $('span.title').each(function () {
                // console.log($(this).text());
                if ($(this).text() == '数量') {
                    shop[$(this).text()] = $(this).siblings('input').val();
                } else {
                    shop[$(this).text()] = $(this).siblings('span.select').text();
                }
            });
            //如果全部选择了信息,则打印,否则,提示重新选择商品属性
            for (key in shop) {
                // console.log(shop[key]);
                if (shop[key] == '') {
                    alert('请选择所有商品属性');
                    return false;
                }
            }
            console.log(shop);
        })
    })
</script>
</html>
运行实例 »
点击 "运行实例" 按钮查看在线实例


批改状态:合格

老师批语:
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学