如何使用 Jquery 在 iframe 中加载 url
我想在点击时加载 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
HTML:
HTML:
尝试
$(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
有时纯 JavaScript 比 jQuery 更酷、更快;-)
Sometimes plain JavaScript is even cooler and faster than jQuery ;-)
这是视图中的 Iframe:
将其加载到脚本中:
here is Iframe in view:
Load it in script:
以防万一有人仍然偶然发现这个老问题:
从某种意义上说,代码理论上几乎是正确的,问题在于使用 $('this') 而不是 $(this),因此告诉 jQuery寻找标签。
脚本本身不能像现在这样工作,因为 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.
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.