convert preload img to iframe

2019-09-13 01:55发布

$('<img />')
   .attr('src', val)
   .attr('width', "400")
   .load(function(){
      $('.showdata').append( $(this) );
   }
);

This works perfectly for images, how would this convert 1:1 to iframes? Here quick jsfiddle: http://jsfiddle.net/ET8Gw/

4条回答
别忘想泡老子
2楼-- · 2019-09-13 02:12
var val = "http://inapcache.boston.com/universal/site_graphics/blogs/bigpicture/texas_drought_fires/bp1.jpg";
$('<iframe/>')
    .attr('src', val)
    .attr('width', "500")
    .appendTo($('.showdata'));

http://jsfiddle.net/ET8Gw/5/

查看更多
Summer. ? 凉城
3楼-- · 2019-09-13 02:12

Try something like this:

$("<iframe></iframe>")
    .css("display":"none")
    .attr('src', val)
    .load( 
        function(){
            $(this).show();
        })
    .appendTo(".showData");

It will not work cross domain.

查看更多
Root(大扎)
4楼-- · 2019-09-13 02:13

I got it working doing this

var val = "http://inapcache.boston.com/universal/site_graphics/blogs/bigpicture/texas_drought_fires/bp1.jpg";

$('.showdata').append('<iframe src="' + val + '" width="400" height="200"><\/iframe>');

jsFiddle: http://jsfiddle.net/ET8Gw/22/

查看更多
迷人小祖宗
5楼-- · 2019-09-13 02:16

Unlike Image objects, IFrames will not load until they are part of the DOM.

To get around this you can hide the element, add it to the DOM, and when the load event fires show it:

var val = "http://dumb.com/";
 $('<iframe />')
            .hide()
            .attr('src', val)
            .attr('width', "500")
            .load(function(){
                $(this).show();
            })
            .appendTo(".showdata");

http://jsfiddle.net/ET8Gw/1/

查看更多
登录 后发表回答