如何使用 Jquery 在 iframe 中加载 url

发布于 2024-11-30 23:21:04 字数 263 浏览 3 评论 0原文

我想在点击时加载 iframe,这就是我到目前为止所拥有的:

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

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

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

盛夏尉蓝 2024-12-07 23:21:04
$("#button").click(function () { 
    $("#frame").attr("src", "http://www.example.com/");
});

HTML:

 <div id="mydiv">
     <iframe id="frame" src="" width="100%" height="300">
     </iframe>
 </div>
 <button id="button">Load</button>
$("#button").click(function () { 
    $("#frame").attr("src", "http://www.example.com/");
});

HTML:

 <div id="mydiv">
     <iframe id="frame" src="" width="100%" height="300">
     </iframe>
 </div>
 <button id="button">Load</button>
半世晨晓 2024-12-07 23:21:04

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

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

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

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

心头的小情儿 2024-12-07 23:21:04
$("#frame").click(function () { 
    this.src="http://www.google.com/";
});

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

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

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

一曲琵琶半遮面シ 2024-12-07 23:21:04

这是视图中的 Iframe:

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

    </iframe>

将其加载到脚本中:

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

here is Iframe in view:

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

    </iframe>

Load it in script:

 $('#ifmReport').attr('src', '/ReportViewer/ReportViewer.aspx');
铁憨憨 2024-12-07 23:21:04

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

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

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

脚本本身不能像现在这样工作,因为 load() 函数本身是一个 AJAX 函数,并且 google 似乎没有明确允许使用 AJAX 加载此页面,但这个方法应该是易于使用,以便通过使用相对路径从您自己的域加载页面。

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.

$(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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文