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

标签 javascript jquery

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/

相关文章:

jquery - 我是否应该担心找不到引用的 *.js 文件?如果是这样,我该怎么办?

javascript - 从溢出 :hidden elements 监听滚动事件

javascript - 发布二进制代码无法从 Jquery ajax 工作

javascript - 在 VueJS 中获取事件监听器元素而不是子元素

javascript - 切换按钮可见性

javascript - Web 应用程序控制台错误 : firebase-auth. js :1 Uncaught Error: Cannot instantiate firebase-auth - be sure to load firebase-app. js 首先

更改 : zero and empty 时的 jquery 输入

javascript - iframe 导致文档高度等于 IOS 设备(chrome 和 safari)上的窗口/视口(viewport)高度

jquery - ckeditor 模糊和对话框

javascript - 一键生成多个ajax调用