如何将SimpleWeather.js部分结果放入不同的Div中

时间:2014-01-29 04:15:48

标签: javascript jquery html weather placement

首先,我不知道javascript / jquery ......还在努力学习。我目前正在开发一个使用SimpleWeather.js在网页上显示天气的项目。但是,我希望结果(即位置,温度,高点/低点等)显示在页面上的不同位置。例如,我希望位置显示在窗口的左上角,当前温度显示在左侧中间,在单独的div中。不幸的是,我似乎无法将结果分开:它们全部加载在一起,不能放入单独的div中。

我的页面设置如下:

<body>
    <div id="container">
        <div id="top-header">
            <h1 id="display-location">Location Here...</h1>
        </div>
    </div>

    <div id="main-body">
        <div id="current-weather-column">
            <h2>Current weather is gonna go here.</h2>
        </div>
    </div>

    <div id="four-day-forecast-area" class="three-col">
        <h2>Rest of forecast/four-day forecast here...</h2>
    </div>

<script>
// Docs at http://simpleweatherjs.com
    $(document).ready(function() {
       $.simpleWeather({
            zipcode: '60605',
            woeid: '', //2357536
            location: '',
            unit: 'f',
            success: function(weather) {
              if(weather.temp > 75) {
                $('body').animate({backgroundColor: '#F7AC57'}, 1500);
              } else {
                $('body').animate({backgroundColor: '#0091c2'}, 1500);
              }
              html = '<h1 class="icon-'+weather.code+'"></h1>';
              html += '<h2 class="weather-temp">'+weather.temp+'&deg;</h2>';
              html += '<ul><li>'+weather.city+', '+weather.region+'</li>';
              html += '<li class="currently">'+weather.currently+'</li></ul>';
              //html += '<li>'+weather.tempAlt+'&deg;C</li></ul>';

              var timestamp = moment(weather.updated);
              html += '<p class="updated">Updated '+moment(timestamp).fromNow()+'</p>';

              $("#weather").html(html);
            },
            error: function(error) {
              $("#weather").html('<p>'+error+'</p>');
            }
          });
        });

<!-- I then list the rest of the script which is fairly long. If needed, you can take a look at http://simpleweatherjs.com -->

</body>

希望这是有道理的。你们给予的任何帮助对我来说意义重大。谢谢!

1 个答案:

答案 0 :(得分:0)

这会产生一个html列表并将其放入一个div(或者你没有在这里展示的某个元素),其中包含天气ID ...所以请把它拿出来:

html = '<h1 class="icon-'+weather.code+'"></h1>';
html += '<h2 class="weather-temp">'+weather.temp+'&deg;</h2>';
html += '<ul><li>'+weather.city+', '+weather.region+'</li>';
html += '<li class="currently">'+weather.currently+'</li></ul>';
//html += '<li>'+weather.tempAlt+'&deg;C</li></ul>';

TO:

$("#display-location").html(weather.city+', '+weather.region);
 $("#current-weather-column").html(weather.currently);

等等

基本上你创建一个html div(或其他元素),给它一个ID,然后使用上面的格式引用该ID($“#ID”) 然后填充它的内部,你使用.html(text_and_html_you_want_in_the_element)