jQuery 给了我这个错误
未捕获的类型错误:无法调用未定义的方法“toLowerCase”
当我在输入中添加此按键事件处理程序时,它开始出现。输入正在从 div 切换到文本字段,但每当我这样做时,我都会调用我的 bindHandlers 函数。
这是整个代码:
Date.prototype.getWeek = function() {
var onejan = new Date(this.getFullYear(),0,1);
return Math.ceil((((this - onejan) / 86400000) + onejan.getDay()+1)/7);
}
function leadingZero(num) {
return num < 10 ? '0'+num:num;
}
function bindHandlers() {
$('div.city').click(function() {
$(this).replaceWith('<input type="text" class="city input" value="'+$(this).text()+'" />');
bindHandlers();
});
$('.city.input').blur(function() { changeCity(); });
$('.city.input').keypress(function(e) { if(e.keyCode == 13) changeCity(); });
}
function changeCity() {
clearTimeout(weatherTimeout);
window.city = $(this).val();
$(this).replaceWith('<div class="city">'+window.city+'</div>');
bindHandlers();
}
function loadWeatherData(city) {
$.getScript('http://api.openweathermap.org/data/2.5/weather?q='+city+'&callback=updateWeather&units=metric');
console.log('Getting weather for city "'+city+'"');
}
function updateTime() {
var d = new Date(),
hours = leadingZero(d.getHours()),
minutes = leadingZero(d.getMinutes()),
seconds = leadingZero(d.getSeconds()),
week = leadingZero(d.getWeek()),
date = leadingZero(d.getDate()),
month = leadingZero(d.getMonth()+1),
year = d.getFullYear().toString().substring(2),
dateString = '<div>'+hours+':'+minutes+':'+seconds+'<br />w'+week+' '+date+'-'+month+'-'+year+'</div>';
$('.date').html(dateString);
setTimeout(updateTime, 1000);
}
function updateWeather(data) {
console.log(data);
$('.weather').fadeOut(function() {
$('.temp').html(data.main.temp+'°');
$('.humid').html(data.main.humidity+'%');
for(var i = 0; i < data.weather.length; i++) {
function wIcon(data) {
switch(data) {
case 'Clear':
return 'ò';
break;
case 'Drizzle':
case 'Rain':
return 'õ';
break;
case 'Thunderstorm':
return 'ú';
break;
case 'Snow':
return 'û';
break;
case 'Clouds':
return 'ö';
break;
}
};
var icon = wIcon(data.weather[i].main);
console.log(icon);
$('.weather-icon').html('');
$('.weather-icon').append('<i data-icon="'+icon+'" title="'+data.weather[i].description+'"></i>')
$('.wind-speed').html(data.wind.speed+' m/s');
}
weatherTimeout = setTimeout(function() {
loadWeatherData();
}, 30 * 1000);
$(this).fadeIn(function() { $('.wind-deg').rotate(data.wind.deg); });
});
}
$(function() {
var city = 'Roskilde', weatherTimeout;
loadWeatherData(city);
updateTime();
bindHandlers();
});