Can I get the image and load via ajax into div

前端 未结 4 1906
星月不相逢
星月不相逢 2020-12-06 08:07

I have the code below and what I need to do is load the image from the href into a div via ajax... Any help appreciated. I believe that load() can\'t load images like this?<

4条回答
  •  我在风中等你
    2020-12-06 08:45

    You have to also remove the currently appended image. Here it is with a click event and Image instead of appending markup.

    $('#list li a').click(function () {
        var url = $(this).attr('href'),
        image = new Image();
        image.src = url;
        image.onload = function () {
            $('#image-holder').empty().append(image);
        };
        image.onerror = function () {
            $('#image-holder').empty().html('That image is not available.');
        }
    
        $('#image-holder').empty().html('Loading...');
    
        return false;
    });
    

提交回复
热议问题