Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/413.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Chrome抛出未捕获的TypeError:无法调用方法';toLowerCase';使用e.keyCode时未定义的_Javascript_Jquery - Fatal编程技术网

Javascript Chrome抛出未捕获的TypeError:无法调用方法';toLowerCase';使用e.keyCode时未定义的

Javascript Chrome抛出未捕获的TypeError:无法调用方法';toLowerCase';使用e.keyCode时未定义的,javascript,jquery,Javascript,Jquery,jQuery给了我这个错误 未捕获的TypeError:无法调用未定义的方法“toLowerCase” 当我在输入中添加这个按键事件处理程序时,它开始出现。 输入正从div切换到文本字段,但每当我这样做时,我都会调用bindHandlers函数 以下是全部代码: Date.prototype.getWeek = function() { var onejan = new Date(this.getFullYear(),0,1); return Math.ceil((((thi

jQuery给了我这个错误

未捕获的TypeError:无法调用未定义的方法“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+'&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();

});
Date.prototype.getWeek=function(){
var onejan=新日期(this.getFullYear(),0,1);
返回Math.ceil(((this-onejan)/86400000)+onejan.getDay()+1)/7);
}
函数前导零(num){
返回数值<10?'0'+num:num;
}
函数bindHandlers(){
$('div.city')。单击(函数(){
$(此)。替换为(“”);
bindHandlers();
});
$('.city.input').blur(函数(){changeCity();});
$('.city.input').keypress(函数(e){if(e.keyCode==13)changeCity();});
}
函数changeCity(){
clearTimeout(天气超时);
window.city=$(this.val();
$(this.replacement为(“”+window.city+“”);
bindHandlers();
}
函数loadWeatherData(城市){
$.getScript('http://api.openweathermap.org/data/2.5/weather?q=“+city+”&callback=updatewather&units=metric”);
console.log('get weather for city'+city+'”);
}
函数updateTime(){
var d=新日期(),
小时数=领先零(d.getHours()),
分钟数=领先零(d.getMinutes()),
秒=引线零(d.getSeconds()),
week=leadingZero(d.getWeek()),
日期=leadingZero(d.getDate()),
月份=领先零(d.getMonth()+1),
year=d.getFullYear().toString()子字符串(2),
dateString=''+hours+':''+minutes+':''+seconds+'
w'+week+''+date+'-'+month+'-'+year+''; $('.date').html(日期字符串); setTimeout(updateTime,1000); } 函数更新器(数据){ 控制台日志(数据); $('.weather').fadeOut(函数(){ $('.temp').html(data.main.temp+'°;'); $('.潮湿').html(data.main.weathy+'%'); 对于(var i=0;i
Aah。。发现了这个问题,因为我都有
blur
keypress
,所以我为它们创建了一个函数,这样我的代码就不会重复,当我创建函数时,我忘记了将$(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();

}

函数bindHandlers(){
$('div.city')。单击(函数(){
$(此)。替换为(“”);
bindHandlers();
});
$('.city.input').blur(函数(){changeCity(this);});
$('.city.input').keypress(函数(e){if(e.keyCode==13)changeCity(this);});
}
功能转换城市(el){
window.city=$(el.val();
$(el).替换为(“”+window.city+“”);
clearTimeout(天气超时);
loadWeatherData(窗口城市);
localStorage.setItem('city',window.city);
bindHandlers();
}

结论:将代码移动到函数中时,确保函数仍然从以前的位置获取因变量。Aah。。发现了这个问题,因为我都有
blur
keypress
,所以我为它们创建了一个函数,这样我的代码就不会重复,当我创建函数时,我忘记了将$(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();

}

函数bindHandlers(){
$('div.city')。单击(函数(){
$(此)。替换为(“”);
bindHandlers();
});
$('.city.input').blur(函数(){changeCity(this);});
$('.city.input').keypress(函数(e){if(e.keyCode==13)changeCity(this);});
}
功能转换城市(el){
window.city=$(el.val();
$(el).替换为(“”+window.city+“”);
clearTimeout(天气超时);
loadWeatherData(窗口城市);
localStorage.setItem('city',window.city);
bindHandlers();
}

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

首先:
e.keyCode
Number
的类型,它没有
toLowerCase()
方法。第二:你的代码片段中没有
e.keyCode.toLowerCase()
。我知道没有toLowerCase(),toLowerCase()在jQuery库中。对不起,我应该更仔细地阅读这个问题,第一句话“
jQuery给我这个错误
”应该能说明一切。也许是jsFiddle?没有html很难调试。现在您自己找到了答案,请发布一个答案并标记。首先:
e.keyCode
Number
类型,它没有
toLowerCase()
方法。第二:你的代码片段中没有
e.keyCode.toLowerCase()
,我知道