Uncaught TypeError: undefined is not a function javascript function

前端 未结 2 1387
旧时难觅i
旧时难觅i 2021-01-16 21:05

I wrote the \"bubble sort\" function to sort a list of images. I can\'t understand why the function returns the \"Uncaught TypeError: undefined is not a function\". Can anyo

相关标签:
2条回答
  • 2021-01-16 21:47
    a[i].attr('src') = a[i+1].attr('src');
    

    has to be written:

    a[i].attr('src', a[i+1].attr('src'));
    

    Same for line below.

    0 讨论(0)
  • 2021-01-16 21:50

    Instead of list[pos] use list.eq(pos) since with the first one you get a raw HTML element (which has no attr function) and the second way you get a jQuery object (which has an attr function)

    Also use list.length instead of list.size() since the size function has been deprecated since version 1.8

    Finally as noted by M. Page

    a[i].attr('src') = a[i + 1].attr('src');
    

    should be

    a.eq(i).attr('src', a.eq(i + 1).attr('src'));
    

    and the same goes for statements like it

    You should also consider altering your getAlt function in the event that the img does not have an alt attribute

    function getAlt(list, pos) {
        var img = list.eq(pos).attr("alt") || "";    
        if(img) {
            img = img.split(' ');
        }
        return img[3] || "";
    }
    

    All of these changes can be found in the following fiddle http://jsfiddle.net/ejhq03qy/1/

    0 讨论(0)
提交回复
热议问题