-2

嗨,我正在尝试使用Wunderground Weather API,但在单击按钮时请求信息时遇到了麻烦。这是提供的工作开场线:

jQuery(document).ready(function($) {

这是我将其更改为:

jQuery('#GetWeather').click(function($) {

当我单击按钮时,我得到一个萤火虫错误:

TypeError: $.ajax is not a function
[Break On This Error]   

success : function(parsed_json) {

我不知道为什么它不起作用,但这里是其余的代码......希望它有帮助:

jQuery('#GetWeather').click(function($) {
var PostCode="PL9 9UT";
$.ajax({ url : "http://api.wunderground.com/api/2508132ae0c7601a/geolookup/conditions/q/UK/"+PostCode +".json",
dataType : "jsonp",
success : function(parsed_json) {

var icon = parsed_json['current_observation']['icon'];
var temp_c = parsed_json['current_observation']['temp_c'];
var feelslike_c = parsed_json['current_observation']['feelslike_c'];
var visibility_mi = parsed_json['current_observation']['visibility_mi'];
var UV = parsed_json['current_observation']['UV'];
var relative_humidity = parsed_json['current_observation']['relative_humidity'];
var wind_mph = parsed_json['current_observation']['wind_mph'];
var pressure_mb = parsed_json['current_observation']['pressure_mb'];
var wind_string = parsed_json['current_observation']['wind_string'];
var weather = parsed_json['current_observation']['weather'];

var imageurl = "http://192.168.0.4/DesktopVersion/Inc/Images/Weather/";

$('.Wicon').css('background-image',"url("+imageurl +icon +".svg)");
$('#GetWeatherTemp').html(temp_c +"&#176");
$('#GetWeatherFeel').html("Feels Like " +feelslike_c +"&#8451");
$('#GetWeatherVis').html(visibility_mi +" Miles");
$('#GetWeatherUv').html(UV);
$('#GetWeatherHumid').html(relative_humidity +"%");
$('#GetWeatherWind').html("Wind Speed " +wind_mph +"Mph");
$('#GetWeatherPress').html(pressure_mb);
$('#GetWeatherState').html(weather);
}
});
});
4

2 回答 2

5

这条线是你的问题:

jQuery('#GetWeather').click(function($) {

document.ready处理程序中,jQuery传入,以便您可以方便地将其别名$为兼容性。但是,对于click处理程序,第一个参数是一个事件对象,这意味着您正在$用事件引用覆盖 jQuery 简写 ( )。

于 2013-01-25T19:25:21.363 回答
3

click事件不发送jQuery对象作为参数,它发送event对象,所以$函数内部不会是jQuery对象,而是event点击事件的对象。

只需删除$参数,使其不会影响全局$标识符:

jQuery('#GetWeather').click(function() {
于 2013-01-25T19:25:43.250 回答