如何使用 Jquery 在 iframe 中加载 url

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7177080/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 23:30:14  来源:igfitidea点击:

How do I load an url in iframe with Jquery

jqueryiframeload

提问by Youss

I want to load an iframe on click, this is what I have so far:

我想在点击时加载一个 iframe,这是我目前所拥有的:

$("#frame").click(function () { 
      $('this').load("http://www.google.com/");
    });

It doesn't work. This is the complete code: JS Bin

它不起作用。这是完整的代码:JS Bin

回答by Dogbert

$("#button").click(function () { 
    $("#frame").attr("src", "http://www.example.com/");
});

HTML:

HTML:

 <div id="mydiv">
     <iframe id="frame" src="" width="100%" height="300">
     </iframe>
 </div>
 <button id="button">Load</button>

回答by Chris

Try $(this).load("/file_name.html");. This method targets a local file.

试试$(this).load("/file_name.html");。此方法针对本地文件。

You can also target remote files (on another domain) take a look at: http://en.wikipedia.org/wiki/Same_origin_policy

您还可以针对远程文件(在另一个域上)查看:http: //en.wikipedia.org/wiki/Same_origin_policy

回答by Christophe

$("#frame").click(function () { 
    this.src="http://www.google.com/";
});

Sometimes plain JavaScript is even cooler and faster than jQuery ;-)

有时,纯 JavaScript 甚至比 jQuery 更酷、更快;-)

回答by Luqman Cheema

here is Iframe in view:

这是 Iframe 视图:

<iframe class="img-responsive" id="ifmReport" width="1090" height="1200" >

    </iframe>

Load it in script:

在脚本中加载它:

 $('#ifmReport').attr('src', '/ReportViewer/ReportViewer.aspx');

回答by Paul Daniel Ghiran

Just in case anyone still stumbles upon this old question:

以防万一有人仍然偶然发现这个老问题:

The code was theoretically almost correct in a sense, the problem was the use of $('this') instead of $(this), therefore telling jQuery to look for a tag.

代码理论上在某种意义上几乎是正确的,问题是使用 $('this') 而不是 $(this),因此告诉 jQuery 查找标签。

$(document).ready(function(){
  $("#frame").click(function () { 
    $(this).load("http://www.google.com/");
  });
});

The script itself woudln't work as it is right now though because the load() function itself is an AJAX function, and google does not seem to specifically allow the use of loading this page with AJAX, but this method should be easy to use in order to load pages from your own domain by using relative paths.

脚本本身不会像现在这样工作,因为 load() 函数本身是一个 AJAX 函数,而且 google 似乎并没有特别允许使用 AJAX 加载这个页面,但这种方法应该很容易使用为了使用相对路径从您自己的域加载页面。