我能得到的图像和负载通过AJAX到DIV

人气:1,036 发布:2022-09-11 标签: jquery ajax image-processing

问题描述

我有以下的code和我需要做的是通过AJAX ...任何帮助AP preciated加载从HREF的图像转换成一个div。我相信,负载()无法加载像这样的图片?

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?

    <ul id="list">
    <li class="list-item-1"><a href="images/image1.jpg">Image 1</a></li>
    <li class="list-item-2"><a href="images/image2.jpg">Image 2</a></li>
    <li class="list-item-3"><a href="images/image3.jpg">Image 3</a></li>
    <li class="list-item-4"><a href="images/image4.jpg">Image 4</a></li>
    </ul>
<div id="image-holder"></div>

非常感谢, ç

Many thanks, C

推荐答案

您也必须删除当前附加的图像。这是一个点击事件,而不是附加标记和图像。

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;
});

210