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

这对图像非常有效,如何将 1:1 转换为 iframe?这里快速jsfiddle:http: //jsfiddle.net/ET8Gw/

4

4 回答 4

3

与 Image 对象不同,IFrame 在成为 DOM 的一部分之前不会加载。

要解决这个问题,您可以隐藏元素,将其添加到 DOM,并在加载事件触发时显示它:

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

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

于 2011-09-12T16:06:13.703 回答
2
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/

于 2011-09-12T16:08:09.483 回答
1

尝试这样的事情:

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

它不会跨域工作。

于 2011-09-12T16:08:29.677 回答
1

我让它工作

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/

于 2011-09-12T16:26:01.640 回答