如何使用jQuery创建iframe,并在页面上显示?

Hai*_* IT 23 jquery

$(document).ready(function(){
    $('#button').click(function(){
       $('.accordion').load('<iframe src="google.com" frameborder="0" scrolling="no" id="myFrame"></iframe>');
    });
});
Run Code Online (Sandbox Code Playgroud)

这无法显示google.com的iframe.任何人都可以提供有关如何修复它的建议吗?

And*_*ose 82

jQuery的加载函数不是这样使用的.它将URL作为参数等,并从该URL加载响应.

如果您正在尝试创建iframe DOM元素,请使用jQuery函数执行此操作并将其追加到您想要的位置:

$('<iframe src="http://google.com" frameborder="0" scrolling="no" id="myFrame"></iframe>')
     .appendTo('.accordion');
Run Code Online (Sandbox Code Playgroud)

要么

$('<iframe>', {
   src: 'http://google.com',
   id:  'myFrame',
   frameborder: 0,
   scrolling: 'no'
   }).appendTo('.accordion');
Run Code Online (Sandbox Code Playgroud)