-2

我想通过 yahoo 的 API 获取天气。函数触发直到html. 我把alerthtml 放在 function 之前。在它html向我显示警报之前。但是当它之后html它不起作用。它显示html有错误。什么问题?

<script type="text/javascript">
    $(document).ready(function () {
        $.simpleWeather({
            zipcode: '',
            woeid: '@Html.DisplayFor(modelItem => item.zipcode)',
            location: '',
            unit: 'c',
            success: function (weather) {
                //html = '<h2>' + weather.city + ', ' + weather.region + '</h2>';
                //html += '<img style="float:left;" width="125px" src="' + weather.image + '">';
                alert("hi");
                html += '<p>' + weather.temp + '&deg; ' + weather.units.temp + '<br /><span>' + weather.currently + '</span></p>';
                html += '<a href="' + weather.link + '">View Forecast &raquo;</a>';

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

现在警报有效

但在此警报中不起作用:

<script type="text/javascript">
    $(document).ready(function () {
        $.simpleWeather({
            zipcode: '',
            woeid: '@Html.DisplayFor(modelItem => item.zipcode)',
            location: '',
            unit: 'c',
            success: function (weather) {
                html = '<h2>' + weather.city + ', ' + weather.region + '</h2>';
                html += '<img style="float:left;" width="125px" src="' + weather.image + '">';
                alert("hi");
                html += '<p>' + weather.temp + '&deg; ' + weather.units.temp + '<br /><span>' + weather.currently + '</span></p>';
                html += '<a href="' + weather.link + '">View Forecast &raquo;</a>';

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

这是我的 html 代码:

<div id="weather"></div>

html有错误。请帮帮我 。

4

4 回答 4

5

您没有初始化也没有声明html,所以这一行不能附加到它:

   html += '<p>' + weather.temp + '&deg; ' + ...

您可以更改html +=var html =.

请注意,第二个代码,即使它“有效”,仍然没有明确声明 html 变量(var缺少关键字),这使其成为全局变量。

于 2013-06-17T14:36:06.177 回答
0

您需要初始化 var html,例如var html = 'blah blah blah'

于 2013-06-17T14:36:56.223 回答
0

您没有将邮政编码传递给 simpleWeather 插件。

zipcode: '',

您必须传递邮政编码值。例子zipcode: '90210',

代码是

$(document).ready(function () {
        $.simpleWeather({
            zipcode: '90210',
            woeid: '@Html.DisplayFor(modelItem => item.zipcode)',
            location: '',
            unit: 'c',
            success: function (weather) {
                html = '<h2>' + weather.city + ', ' + weather.region + '</h2>';
                html += '<img style="float:left;" width="125px" src="' + weather.image + '">';
                alert("hi");
                html += '<p>' + weather.temp + '&deg; ' + weather.units.temp + '<br /><span>' + weather.currently + '</span></p>';
                html += '<a href="' + weather.link + '">View Forecast &raquo;</a>';

                $("#weather").html(html);
            },
            error: function (error) {
                $("#weather").html('<p>' + error + '</p>');
            }
        });
    });
于 2013-06-17T14:43:34.463 回答
0

试试我们的简易天气 API怎么样?

这是一个小的jQuery 天气示例:http: //jsbin.com/isukam/1

如果您需要帮助,请告诉我。

披露:我拥有这个 API。

于 2013-06-17T20:50:45.233 回答