如何在div中动态插入iframe [英] How do I dynamically insert an iframe in a div

查看:120
本文介绍了如何在div中动态插入iframe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用jQuery在div中动态插入iframe?

How do I dynamically insert an iframe in a div using jQuery?

$(document).ready(function() {
    $("div").html("<iframe src='http://google.com'><iframe>");  
});

我尝试了上面的代码,但它不起作用。

I tried the above code, but it doesn't work.

推荐答案

您的工作原理: http:// jsfiddle。 net / cUSVj /

虽然请注意,如果它不在同一个域中,那么在创建它之后你就​​无法做很多事情,这是应该的根据同源政策实施的限制。

Though keep in mind you can't do much with it after it's created if it's in not in the same domain, this is due to restrictions in place per the same-origin policy.

编辑:我正在关闭标记,认为这是一个粘贴错误,你确实在<$ c中错过了 / $ c>< / iframe> 结束标记...这将/将无效,具体取决于浏览器的丰富程度。确保正确关闭它以使您的HTML有效,否则您将遇到跨浏览器问题,如果它可以开始使用。

I was closing the tag thinking it was a paste error, you are indeed missing a / in your </iframe> closing tag...this will/won't work depending on how generous the browser is. Make sure to close it correctly so your HTML is valid, otherwise you'll have cross-browser issues, if it works to begin with.

这篇关于如何在div中动态插入iframe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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