最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery Uncaught TypeError: Cannot read property 'nodeType' of undefined - Stack Overflow

programmeradmin1浏览0评论

There are 4 of these all the same. I narrowed it down to this line by a process of elimination:

originalImg = $(element).find('.ProductImage a img').attr();

I'm at a bit of a loss of what might be causing this.

Here is my code in full:

$(function(){
    $('.SubCategoryList li').each(function(index, element) {
        $(element).append('<div class="ProductListWrapper"></div>')
        var subcategory_link = $(element).find('a').attr('href');
        $(element)
            .find('.ProductListWrapper')
            .load( subcategory_link + ' #CategoryContent ul.ProductList', productHovers);
    });
});

function productHovers(){
    $('.SubCategoryList .ProductList li').each(function(index, element){

        // This is the problem line. 
        originalImg = $(element).find('.ProductImage a img').attr(); 

        $(element).mouseover(function(){
           var link = $(this).find(".ProductImage a").attr('href');
           $.get(link,function(data,status){
               var tinyImg = $(data)
                   .find('.ImageCarouselBox ul li:nth-child(1) img')
                   .attr('src');
               var imageSRC = tinyImg.replace('.60.60.jpg','.200.200.jpg');
               $(element).find('.ProductImage a img').attr('src',imageSRC);
           });
        });
        $(element).mouseleave(function(){
             $(element).find('.ProductImage a img').attr('src',originalImg);
        });
    });
}

There are 4 of these all the same. I narrowed it down to this line by a process of elimination:

originalImg = $(element).find('.ProductImage a img').attr();

I'm at a bit of a loss of what might be causing this.

Here is my code in full:

$(function(){
    $('.SubCategoryList li').each(function(index, element) {
        $(element).append('<div class="ProductListWrapper"></div>')
        var subcategory_link = $(element).find('a').attr('href');
        $(element)
            .find('.ProductListWrapper')
            .load( subcategory_link + ' #CategoryContent ul.ProductList', productHovers);
    });
});

function productHovers(){
    $('.SubCategoryList .ProductList li').each(function(index, element){

        // This is the problem line. 
        originalImg = $(element).find('.ProductImage a img').attr(); 

        $(element).mouseover(function(){
           var link = $(this).find(".ProductImage a").attr('href');
           $.get(link,function(data,status){
               var tinyImg = $(data)
                   .find('.ImageCarouselBox ul li:nth-child(1) img')
                   .attr('src');
               var imageSRC = tinyImg.replace('.60.60.jpg','.200.200.jpg');
               $(element).find('.ProductImage a img').attr('src',imageSRC);
           });
        });
        $(element).mouseleave(function(){
             $(element).find('.ProductImage a img').attr('src',originalImg);
        });
    });
}
Share Improve this question edited Dec 24, 2019 at 20:14 ruffin 17.5k10 gold badges95 silver badges149 bronze badges asked Feb 12, 2014 at 9:10 Jack PilowskyJack Pilowsky 2,3035 gold badges31 silver badges41 bronze badges 1
  • As a general rule when an uncaught error is generated inside jQuery, please precise the version you use. – Denys Séguret Commented Feb 12, 2014 at 9:14
Add a comment  | 

1 Answer 1

Reset to default 14

You probably meant

originalImg = $(element).find('.ProductImage a img').attr('src');

The attr function isn't designed to be called without arguments.

发布评论

评论列表(0)

  1. 暂无评论