Load image with jQuery and append it to the DOM

前端 未结 6 1048
执念已碎
执念已碎 2020-11-27 03:54

I\'m trying to load an image from a given link

var imgPath = $(imgLink).attr(\'href\');

and append it to the page, so I can insert it i

相关标签:
6条回答
  • 2020-11-27 04:08
    $('<img src="'+ imgPath +'">').load(function() {
      $(this).width(some).height(some).appendTo('#some_target');
    });
    

    If you want to do for several images then:

    function loadImage(path, width, height, target) {
        $('<img src="'+ path +'">').load(function() {
          $(this).width(width).height(height).appendTo(target);
        });
    }
    

    Use:

    loadImage(imgPath, 800, 800, '#some_target');
    
    0 讨论(0)
  • 2020-11-27 04:14
    var img = new Image();
    
    $(img).load(function(){
    
      $('.container').append($(this));
    
    }).attr({
    
      src: someRemoteImage
    
    }).error(function(){
      //do something if image cannot load
    });
    
    0 讨论(0)
  • 2020-11-27 04:16

    I imagine that you define your image something like this:

    <img id="image_portrait" src="" alt="chef etat"  width="120" height="135"  />
    

    You can simply load/update image for this tag and chage/set atts (width,height):

    var imagelink;
    var height;
    var width;
    $("#image_portrait").attr("src", imagelink);
    $("#image_portrait").attr("width", width);
    $("#image_portrait").attr("height", height);
    
    0 讨论(0)
  • 2020-11-27 04:17

    With jQuery 3.x use something like:

    $('<img src="'+ imgPath +'">').on('load', function() {
      $(this).width(some).height(some).appendTo('#some_target');
    });
    
    0 讨论(0)
  • 2020-11-27 04:21

    Here is the code I use when I want to preload images before appending them to the page.

    It is also important to check if the image is already loaded from the cache (for IE).

        //create image to preload:
        var imgPreload = new Image();
        $(imgPreload).attr({
            src: photoUrl
        });
    
        //check if the image is already loaded (cached):
        if (imgPreload.complete || imgPreload.readyState === 4) {
    
            //image loaded:
            //your code here to insert image into page
    
        } else {
            //go fetch the image:
            $(imgPreload).load(function (response, status, xhr) {
                if (status == 'error') {
    
                    //image could not be loaded:
    
                } else {
    
                    //image loaded:
                    //your code here to insert image into page
    
                }
            });
        }
    
    0 讨论(0)
  • 2020-11-27 04:24

    after you get the image path, try either of following ways

    1. (as you need to set more attr than just the src) build the html and replace to the target region

      $('#target_div').html('<img src="'+ imgPaht +'" width=100 height=100 alt="Hello Image" />');
      
    2. you may need to add some delay if changing the "SRC" attr

      setTimeout(function(){///this function fire after 1ms delay
            $('#target_img_tag_id').attr('src',imgPaht);
      }, 1);
      
    0 讨论(0)
提交回复
热议问题