Changing the image source using jQuery

前端 未结 16 2074
天命终不由人
天命终不由人 2020-11-22 01:24

My DOM looks like this:

16条回答
  •  难免孤独
    2020-11-22 02:21

    One of the common mistakes people do when change the image source is not waiting for image load to do afterward actions like maturing image size etc. You will need to use jQuery .load() method to do stuff after image load.

    $('yourimageselector').attr('src', 'newsrc').load(function(){
        this.width;   // Note: $(this).width() will not work for in memory images
    
    });
    

    Reason for editing: https://stackoverflow.com/a/670433/561545

提交回复
热议问题