day31-Ajax

一、Ajax获取数据

1.Ajax

HTML: Hyper-Text Markup Language 。
XML: eXtensible Markup Language 。
XML最为重要的用途是在两个异构的系统之间交换数据 。
现在这项功能基本上被JSON和YAML格式替代了 。
Ajax: Asynchronous JavaScript and XML 。
通过JavaScript代码向服务器发起异步请求并获得数据 。
异步请求:在不中断用户体验的前提下向服务器发出请求 。
获得数据后可以通过DOM操作对页面进行局部刷新加载服务器返回的数据。

代码

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Ajax获取数据</title>
    </head>
    <body>
         <button id="ok">换一组</button>
         <div id="container"></div>
         
         <script src="js/jquery.min.js" type="text/javascript" charset="utf-8"></script>
         <script type="text/javascript">
            $(function(){
                $('#ok').on('click',function(){
                    var url = 'http://api.tianapi.com/meinv/?key=b8a234b84684732e96dd79e512b685f8&num=10';
                    $.getJSON(url, function(jsonObj){
                        $('#container').empty();
                        $.each(jsonObj.newslist, function(index,elem) {
                            $('#container').append(
                                $('<img>').attr('width','400').attr('height','550').attr('src',elem.picUrl)
                            );
                        });
                    });
                });
            });
         </script>
    </body>
</html>


2.周公解梦

代码

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>周公解梦</title>
        <style type="text/css">
            #search{
                width: 740px;
                margin: 0 auto;
                text-align: center;
                margin-top: 150px;
            }
            
            #keyWord{
                display: inline-block;
                width: 520px;
                height: 36px;
                font: 36px/36px arial;
                text-align: center;
                vertical-align: middle;
                border: none;
                outline: none;
                border-bottom: 1px dotted darkgray;
            }
            
            
            /*#search input[type=button] -> 属性选择器*/
            #button1{
                width: 80px;
                height: 36px;
                font:20px/36px arial;
                border: none;
                color: white;
                background-color: green;
                vertical-align: middle;
            }
            
            #result{
                width: 640px;
                margin: 0 auto;
                font: 20px/20px arial;
            }
        </style>
    </head>
    <body>
        
        <div id="search">
            <input type="text" id="keyWord" value="" placeholder="请输入关键字"/>
            <input type="button" id="button1" value="查询" />
        </div>
        <hr>
        <p id="result"></p>
        
        <script src="js/jquery.min.js" type="text/javascript" charset="utf-8"></script>
        <script type="text/javascript">
            //将URL不能识别的字符转化成能够识别的%编码
            //encodeURIComponent(str,'utf-8');
            //将%编码的数据转化成普通的字符
            //decodeURIComponent(str,'utf-8');
            
            $(function(){
                $('#button1').on('click',function(){
                    var keyWords = $('#keyWord').val().trim();
                    if(keyWords.length > 0){
                        $('#result').empty();
                        $.ajax({
                            'url': 'http://api.tianapi.com/txapi/dream/',
                            'type': 'get',
                            'data': {
                                'key': 'b8a234b84684732e96dd79e512b685f8',
                                'word': keyWords
                            },
                            'dataType': 'json',
                            'success': function(jsonObj){
                                if(jsonObj.code == '250'){
                                    $('#result').text(jsonObj.msg);
                                }else{
                                    $('#result').text(jsonObj.newslist[0].result);
                                }
                            }
                        });
                    }
                });
            });
        </script>
    </body>
</html>

测试结果

1.PNG

3.购物车

代码


<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>购物车</title>
        <style>
            * { 
                margin: 0;
                padding: 0;
            }
            body {
                width: 960px;
                margin: 20px auto;
            }
            #cart {
                margin: 0 auto;
                width: 850px;
            }
            #cart-header {
                height: 40px;
                background-color: lightgray;
                margin-bottom: 20px;
            }
            #cart-header div {
                line-height: 40px;
            }
            .left {
                float: left;
            }
            .right {
                float: right;
            }
            .w110 {
                width: 100px;
            }
            .ml10 {
                margin-left: 10px;
            }
            .w120 {
                width: 120px;
            }
            .w250 {
                width: 250px;
            }
            .center {
                text-align: center;
            }
            .w20 {
                width: 20px;
            }
            .w90 {
                width: 90px;
            }
            .clear {
                clear: both;
            }
            #cart-items>div {
                height: 100px;
            }
            #cart-items>div>div {
                line-height: 100px;
            }
            .w250 span {
                display: inline-block;
                font-size: 12px;
                line-height: 16px !important;
            }
            .single-item {
                border-bottom: 1px solid gray;
            }
            .small-button {
                display: inline-block;
                width: 20px;
                height: 20px;
                border: none;
            }
            .big-button {
                color: white;
                background-color: red;
                display: inline-block;
                width: 120px;
                height: 40px;
                border: none;
                font-size: 22px;
            }
            #totalCount, #totalPrice {
                color: red;
            }
            #totalPrice {
                font: bolder 20px Arial;
                display: inline-block;
                width: 150px;
            }
            #cart a {
                text-decoration: none;
            }
            #cart a:link, #cart a:visited, #cart a:active {
                color: gray;
            }
        </style>
    </head>
    <body>
        <div id="cart">
            <div id="cart-header">
                <div class="left w110 ml10">
                    <input id="selectAll" type="checkbox" >
                    <label for="selectAll">全选</label>
                </div>
                <div class="left w250">商品</div>
                <div class="left w120 center">单价</div>
                <div class="left w120 center">数量</div>
                <div class="left w120 center">小计</div>
                <div class="left w120 center">操作</div>
            </div>
            <div id="cart-items">
                <div class="clearSingle-item">
                    <div class="left w20 ml10">
                        <input class="selectOne" name="selectOne" type="checkbox">
                    </div>
                    <div class="left w90">
                        <a href="">
                            <img src="img/a1.jpg">
                        </a>
                    </div>
                    <div class="left w250">
                        <span>
                        海澜之家/Heilan Home春装商务白衬衫男修身HNCAD3A067Y 漂白(69) 漂
                        </span>
                    </div>
                    <div class="left w120 center">&yen;<span id="price1" class="price">138.00</span></div>
                    <div class="left w120 center">
                        <button class="small-button">-</button>
                        <input id="input1" class="center-count" type="text" size="2" value="1">
                        <button class="small-button">+</button>
                    </div>
                    <div class="left w120 center">&yen;<span>138.00</span></div>
                    <div class="left w120 center">
                        <a class="del" href="javascript:void(0);">删除</a>
                    </div>
                </div>
                <div class="clear single-item">
                    <div class="left w20 ml10">
                        <input class="selectOne" name="selectOne" type="checkbox">
                    </div>
                    <div class="left w90">
                        <a href="">
                            <img src="img/a2.jpg">
                        </a>
                    </div>
                    <div class="left w250">
                        <span>
                        HLA海澜之家长袖衬衫男牛津纺休闲干净透气HNEAJ1E048A浅灰
                        </span>
                    </div>
                    <div class="left w120 center">&yen;<span id="price2" class="price">128.00</span></div>
                    <div class="left w120 center">
                        <button class="small-button">-</button>
                        <input id="input2" class="center-count" type="text" size="2" value="1">
                        <button class="small-button">+</button>
                    </div>
                    <div class="left w120 center">&yen;<span>128.00</span></div>
                    <div class="left w120 center">
                        <a class="del" href="javascript:void(0);">删除</a>
                    </div>
                </div>
                <div class="clear single-item">
                    <div class="left w20 ml10">
                        <input class="selectOne" name="selectOne" type="checkbox">
                    </div>
                    <div class="left w90">
                        <a href="">
                            <img src="img/a3.jpg">
                        </a>
                    </div>
                    <div class="left w250">
                        <span>
                        HLA海澜之家牛津纺清新休闲衬衫2018春季新品质感柔软长袖衬衫男
                        </span>
                    </div>
                    <div class="left w120 center">&yen;<span id="price3" class="price">99.00</span></div>
                    <div class="left w120 center">
                        <button class="small-button">-</button>
                        <input id="input3" class="center-count" type="text" size="2" value="1">
                        <button class="small-button">+</button>
                    </div>
                    <div class="left w120 center">&yen;99.00</div>
                    <div class="left w120 center">
                        <a class="del" href="javascript:void(0);">删除</a>
                    </div>
                </div>
            </div>
            <div id="cart-footer">
                <div class="clear left">
                    <a id="clearSelected" href="javascript:void(0);">删除选中商品</a>
                </div>
                <div class="right">
                    <span>总共选中了<span id="totalCount">0</span>件商品</span>
                    <span>总计: <span id="totalPrice">&yen;<span id="price">0.00</span></span>
                    <button id="pay" class="big-button">去结算</button>
                </div>
            </div>
        </div>
        
        <script src="js/jquery.min.js" type="text/javascript" charset="utf-8"></script>
        <script type="text/javascript">
            
            $(function(){
                //全选功能
                var checkBoxs = $('.selectOne');
                $.each(checkBoxs, function(index,elem1) {
                    $(elem1).on('click',function(){
                        $.each(checkBoxs, function(index,elem2) {
                            if($(elem2).is(':checked') == false){
                                $('#selectAll').prop({
                                    'checked' : false
                                });
                                return false
                            }
                        $('#selectAll').prop({
                            'checked' : true
                        });
                        });
                    });
                });
                $.each(checkBoxs, function(index,elem) {
                        if($(elem).is(':checked') == false){
                            $('#selectAll').prop({
                                'checked' : false
                            });
                        }else{
                            $('#selectAll').prop({
                                'checked' : true
                            });
                        }
                });
                $('#selectAll').on('click',function(){
                    var allTag = ($('#selectAll').is(':checked'));
                    if(allTag){
                        $.each(checkBoxs, function(index,elem) {
                            $(elem).prop({
                                'checked' : true
                            });
                        });
                    }else{
                        $.each(checkBoxs, function(index,elem) {
                            $(elem).prop({
                                'checked' : false
                            });
                        });
                    }
                });             
            });
            
            //商品件数和总计
            $('#input1').bind('input propertychange',function(evt){
                if($('#input1').val() > 200){
                    $('#input1').val(200);
                }else if($('#input1').val() <= 0){
                    $('#input1').val(0);
                }
            });
            
            $("#input2").bind("input propertychange",function(evt){
                if($('#input1').val() > 200){
                    $('#input1').val(200);
                }else if($('#input1').val() <= 0){
                    $('#input1').val(0);
                }
            });
            
            $("#input3").bind("input propertychange",function(evt){
                if($('#input1').val() > 200){
                    $('#input1').val(200);
                }else if($('#input1').val() <= 0){
                    $('#input1').val(0);
                }
            });

            $('.small-button').on('click',function(evt){
                var num = parseInt($(evt.target).parent().children('input').val());
                    if($(evt.target).text() == '+'){
                        if(num >= 200){
                            $(evt.target).parent().children('input').val(200);
                        }else{
                            $(evt.target).parent().children('input').val(num+1);
                        }
                    }else{
                        if(num<=0){
                            $(evt.target).parent().children('input').val(0);
                        }else{
                            $(evt.target).parent().children('input').val(num-1);
                        }
                    }
            });
            
            
            $('.selectOne').on('click',function(evt){
                var input = $(evt.target).parent().parent().find('input')[1];
                var price = $(evt.target).parent().parent().find('.price');
                if($(evt.target).is(':checked') == true){
                    var total = parseInt($('#totalCount').text()) + parseInt($(input).val())
                    var totalPrice = parseFloat($('#price').text()) + parseInt($(input).val()) * parseFloat($(price).text());
                    $('#price').text(totalPrice);
                    $('#totalCount').text(total);
                }else{
                    var total = parseInt($('#totalCount').text()) - parseInt($(input).val())
                    var totalPrice = parseFloat($('#price').text()) - parseInt($(input).val()) * parseFloat($(price).text());
                    $('#price').text(totalPrice);
                    $('#totalCount').text(total);
                    
                }
            });
            
            $('#selectAll').on('click',function(evt){
                var total = 0;
                totalPrice = 0;
                var price = $(evt.target).parent().parent().find('.price');
                if($(evt.target).is(':checked') == true){
                    $.each($('.center-count'),function(index,elem){
                        total = total + parseInt($(elem).val());
                        totalPrice = $('#input1').val() * parseFloat($('#price1').text()) + $('#input2').val() * parseFloat($('#price2').text()) + $('#input3').val() * parseFloat($('#price3').text());
                    });
                    $('#totalCount').text(total);
                    $('#price').text(totalPrice);
                }else{
                    $('#totalCount').text(0);
                    $('#price').text(0.00);
                }
            });
            
            //删除选中商品
            $('#clearSelected').on('click',function(){
                $.each($('.selectOne'),function(index,elem){
                    if($(elem).is(':checked') == true){
                        var totalPrice = parseFloat($('#price').text()) - $(elem).parent().parent().find('.center-count').val() * $(elem).parent().parent().find('.price').text();
                        var totalCount = $('#totalCount').text() - $(elem).parent().parent().find('.center-count').val();
                        $('#totalCount').text(totalCount);
                        $('#price').text(totalPrice);
                        $(elem).parent().parent().remove()  
                    }
                })
            });
            
            //删除整行
            $('.del').on('click',function(evt){
                var flag = window.confirm('确定要删除该商品吗?');
                if(flag){
                    $(evt.target).parent().parent().remove();
                }
            });
            
        </script>
    </body>
</html>

测试结果

2.PNG

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,185评论 6 503
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,652评论 3 393
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,524评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,339评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,387评论 6 391
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,287评论 1 301
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,130评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,985评论 0 275
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,420评论 1 313
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,617评论 3 334
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,779评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,477评论 5 345
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,088评论 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,716评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,857评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,876评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,700评论 2 354

推荐阅读更多精彩内容