如何使用 jQuery 创建 iframe,并在页面上显示?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8179703/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create an iframe using jQuery, and display on page?
提问by Hai Truong IT
$(document).ready(function(){
$('#button').click(function(){
$('.accordion').load('<iframe src="google.com" frameborder="0" scrolling="no" id="myFrame"></iframe>');
});
});
This can't display the iframe of google.com. Can anyone provide suggestions on how to fix it?
这无法显示 google.com 的 iframe。任何人都可以提供有关如何修复它的建议吗?
回答by Andy Rose
jQuery's load functionisn't used this way. It takes a URL as a parameter, among others, and loads the response from that URL.
jQuery 的加载函数不是这样使用的。它接受一个 URL 作为参数,并从该 URL 加载响应。
If you are trying to create an iframe DOM element, use the jQueryfunction to do this and append it where you want:
如果您尝试创建 iframe DOM 元素,请使用jQuery函数执行此操作并将其附加到您想要的位置:
$('<iframe src="http://google.com" frameborder="0" scrolling="no" id="myFrame"></iframe>')
.appendTo('.accordion');
or
或者
$('<iframe>', {
src: 'http://google.com',
id: 'myFrame',
frameborder: 0,
scrolling: 'no'
}).appendTo('.accordion');
回答by kamil
If .accordion is a container for your iframe try this instead:
如果 .accordion 是 iframe 的容器,请尝试以下操作:
$(document).ready(function(){
$('#button').click(function(){
$('.accordion').html('<iframe src="google.com" frameborder="0" scrolling="no" id="myFrame"></iframe>');
});
});
});
And fix that quote syntax :)
并修复该引用语法:)
回答by Moe Sweet
2 errors.
2 错误。
- Use full path (not sure it's necessary)
close google.com with double quotes.
$('.accordion').load( '< iframe src="http://www.google.com" frameborder="0" scrolling="no" id="myFrame">');
- 使用完整路径(不确定是否有必要)
用双引号关闭 google.com。
$('.accordion').load('< iframe src="http://www.google.com" frameborder="0" scrolling="no" id="myFrame">');
(Ignore the space in iframe tag)
(忽略 iframe 标签中的空格)