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

时间:2021-08-04 01:19:10
$(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。任何人都可以提供有关如何修复它的建议吗?

3 个解决方案

#1


69  

jQuery's load function isn'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 jQuery function 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');

#2


2  

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 :)

并修复引用语法:)

#3


0  

2 errors.

2个错误。

  1. Use full path (not sure it's necessary)
  2. 使用完整路径(不确定是否必要)
  3. close google.com with double quotes.

    用双引号关闭google.com。

    $('.accordion').load( '< iframe src="http://www.google.com" frameborder="0" scrolling="no" id="myFrame">');

    $('。accordion')。load('

(Ignore the space in iframe tag)

(忽略iframe标记中的空格)

#1


69  

jQuery's load function isn'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 jQuery function 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');

#2


2  

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 :)

并修复引用语法:)

#3


0  

2 errors.

2个错误。

  1. Use full path (not sure it's necessary)
  2. 使用完整路径(不确定是否必要)
  3. close google.com with double quotes.

    用双引号关闭google.com。

    $('.accordion').load( '< iframe src="http://www.google.com" frameborder="0" scrolling="no" id="myFrame">');

    $('。accordion')。load('

(Ignore the space in iframe tag)

(忽略iframe标记中的空格)