zoukankan      html  css  js  c++  java
  • 通过jquery获取天气的方法

    代码为:

    $.getScript('http://int.dpool.sina.com/iplookup/iplookup.php?format=js',function(_result){

      if(remote_ip_info.ret=='1'){

         $.ajax({

             type:"GET",

             url:"http://wthrcdn.etouch.cn/weather_mini?city="+remote_ip_info.city,

             data:'',

             success:function(msg){

                  console.log(msg);

               }

            });

       }

    });

    msg为一个字符串,里面包含了城市,5天的天气预报等信息。如果需要在页面进行显示,则在页面获取元素后,通过html()来进行设置内容。msg需要先通过eval()方法或者JSON.parse()方法来转换为JSON对象。然后对msg.data.*获取需要的属性值。如我设置了一个id为a的div,使里面显示天气内容:

    var r=eval('('+msg+')');    //也可以使用JSON.parse(msg);
    $('#a').html(r.data.city+"   "+r.data.forecast[0].date+r.data.forecast[0].fengli+r.data.forecast[0].high+r.data.forecast[0].low+r.data.forecast[0].type);
    console.log(r);

  • 相关阅读:
    C#博客记录二
    C#博客记录一
    label语句
    css选择器
    关于访问对象属性的小问题
    特殊符号unicode编码
    不换行
    正则表达式中的exec()方法
    正则表达式中两种定义方式中的反斜杠
    js删除对象数组
  • 原文地址:https://www.cnblogs.com/lionisnotkitty/p/6109471.html
Copyright © 2011-2022 走看看