Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.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
自定义jQuery插件显示错误_Jquery - Fatal编程技术网

自定义jQuery插件显示错误

自定义jQuery插件显示错误,jquery,Jquery,我创建了自己的微型jQuery插件来更改悬停时的图像src $.fn.hover_image = function () { $(this).each(function () { var img = $(this).children('img'); var oldImgSrc = img.attr('src'); var newImgSrc = oldImgSrc.substr(0, oldImgSrc.lastIndexOf('.')) + '_hover.png';

我创建了自己的微型jQuery插件来更改悬停时的图像src

$.fn.hover_image = function () {
$(this).each(function () {
    var img = $(this).children('img');
    var oldImgSrc = img.attr('src');
    var newImgSrc = oldImgSrc.substr(0, oldImgSrc.lastIndexOf('.')) + '_hover.png';

    $(this).hover(function () {
        img.fadeOut('fast', function () {
            img.attr('src', newImgSrc);
            img.fadeIn('fast');
        });
    },

    function () {
        img.fadeOut('fast', function () {
            img.attr('src', oldImgSrc);
            img.fadeIn('fast');
        });
    });
    return $(this);
});
};
插件工作正常,但我在控制台出错 未捕获的TypeError:无法调用未定义的方法“lastIndexOf”
代码怎么了?如何修复它?

如果出现此错误,那么oldImgSrc未定义,这意味着$this.children'img';返回一个空集,或者第一个选定的元素没有src属性。非常感谢您的评论,我只是尝试将我的函数用于错误的图像。