gpt4 book ai didi

javascript - Chrome 抛出 Uncaught TypeError : Cannot call method 'toLowerCase' of undefined when using e. keyCode

转载 作者:行者123 更新时间:2023-11-28 02:09:26 24 4
gpt4 key购买 nike

jQuery 给了我这个错误

Uncaught TypeError: Cannot call method 'toLowerCase' of undefined

当我在输入上添加此按键事件处理程序时,它开始出现。输入从 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+'&deg;');
$('.humid').html(data.main.humidity+'%');

for(var i = 0; i < data.weather.length; i++) {

function wIcon(data) {
switch(data) {

case 'Clear':
return '&ograve;';
break;

case 'Drizzle':
case 'Rain':
return '&otilde;';
break;

case 'Thunderstorm':
return '&uacute;';
break;

case 'Snow':
return '&ucirc;';
break;

case 'Clouds':
return '&ouml;';
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();

});

最佳答案

啊..发现了这个问题,因为我都有 blurkeypress,我为它们做了一个函数,这样我的代码就不会重复,当我函数,我忘记将 $(this) 解析为函数,所以它不知道我所说的 $(this) 是什么意思。现代码如下:

function bindHandlers() {

$('div.city').click(function() {

$(this).replaceWith('<input type="text" class="city input" value="'+$(this).text()+'" />');
bindHandlers();

});

$('.city.input').blur(function() { changeCity(this); });

$('.city.input').keypress(function(e) { if(e.keyCode == 13) changeCity(this); });

}

function changeCity(el) {

window.city = $(el).val();
$(el).replaceWith('<div class="city">'+window.city+'</div>');

clearTimeout(weatherTimeout);
loadWeatherData(window.city);
localStorage.setItem('city', window.city);

bindHandlers();

}

结论:将代码移动到函数中时,请确保该函数仍然从之前的位置获取因变量。

关于javascript - Chrome 抛出 Uncaught TypeError : Cannot call method 'toLowerCase' of undefined when using e. keyCode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17319695/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com