jQuery 添加带有内容的 iFrame [英] jQuery add iFrame with content

查看:17
本文介绍了jQuery 添加带有内容的 iFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的问题.我需要在fancybox中打开iframe(我需要iframe,因为我需要浏览打开的文档中的链接并留在fancybox中),但我想将内容从变量放入 iframe,而不是通过 src 属性(我已经通过 AJAX 获得了内容(我需要在将内容放入 iframe 之前对内容进行一些检查,所以没有办法代替 AJAX查询),所以我不需要再查询一次).

I've such a problem. I need to open iframe in fancybox (I require iframe because I need to browse through links in the opened document and stay in fancybox), but I want to put content into iframe from variable, not through src attribute (I've already got content by AJAX (I need to do some checks on content before putting it to iframe, so there are no way to do it instead of AJAX query), so I do not need one more query).

Fancybox 不允许将 'content' 属性与 'type':'iframe' 一起使用.所以我决定动态创建 iframe,将我的内容插入其中,并通过fancybox 将 iframe 显示为常规块.

Fancybox does not allow to use 'content' attribute with 'type':'iframe'. So I decided to create iframe dynamically, insert my content into it, and show iframe by fancybox as a regular block.

就像

jQuery('<iframe id="someId"/>').appendTo('body').contents().find('body').append(content);

然后

jQuery('<iframe id="someId"/>').fancybox();

但是第一部分不起作用.我可以看到添加到页面但没有任何内容的 iframe(我在可变内容中有完整的 HTML 页面,但是当我尝试仅附加一些文本时,它不起作用).

But the first part does not work. I can see the iframe that was added to page but without any content (I have full HTML page in variable content, but when I try to append just some text it doesn't work as well).

我做错了什么?

也许还有其他方法可以满足我的需求?

Maybe there is another way to do what I need?

推荐答案

你的问题在调用jquery的ready方法之后就解决了

your problem will be solve after calling jquery's ready method like this

<script type="text/javascript">
$(function(){
$('<iframe id="someId"/>').appendTo('body');
$('#someId').contents().find('body').append('<b>hello sajjad</b>');
});
</script>

<iframe id="someId"></iframe>

这篇关于jQuery 添加带有内容的 iFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆