使用 jquery 更改 iframe 源
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2930315/
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
changing iframe source with jquery
提问by Casey
I've been trying this for a bit now and have looked at other answers to similar questions on SO, but when I am trying to change the src attribute of an iframe, it updates it for the whole window. Here is the following code I am using that works correctly (no jquery):
我已经尝试了一段时间,并查看了关于 SO 类似问题的其他答案,但是当我尝试更改 iframe 的 src 属性时,它会针对整个窗口更新它。这是我正在使用的以下代码,它可以正常工作(没有 jquery):
<html>
<head>
<style type="text/css">
iframe#ifrm {
border:none;
padding:.5em;
margin:1.5em 0 1em;
width:100%;
height:100%;
}
</style>
<script src="./js/jquery-1.4.2.js" type="text/javascript"></script>
<script type="text/javascript">
// <![CDATA[
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// this is the function I'm trying to replace:
function loadIframe(iframeName, url) {
if ( window.frames[iframeName] ) {
window.frames[iframeName].location = url;
return false;
}
return true;
}
// ]]>
</script>
</head>
<body>
<ul>
<li><a href="http://www.google.com/" onclick="return loadIframe('ifrm', this.href)">Page 1</a> </li>
<li><a href="tabs.html" onclick="return loadIframe('ifrm', this.href)">Page 2</a></li>
</ul>
<div class="iframe">
<iframe name="ifrm" id="ifrm" src="tabs.html" frameborder="0">
Your browser doesn't support iframes.</iframe>
As I said, I've tried
正如我所说,我已经尝试过
$('#ifrm').attr('src', "http://www.google.com")
which displays the page, but not in the iframe. I'm really just learning jquery, but I can't figure out what's different about my situation than other similar questions like this:
它显示页面,但不在 iframe 中。我真的只是在学习 jquery,但我无法弄清楚我的情况与其他类似问题有什么不同:
Thanks.
谢谢。
回答by user113716
Should work.
应该管用。
Here's a working example:
这是一个工作示例:
function loadIframe(iframeName, url) {
var $iframe = $('#' + iframeName);
if ( $iframe.length ) {
$iframe.attr('src',url);
return false;
}
return true;
}
回答by TheLegs
Using attr() pointing to an external domain may trigger an error like this in Chrome: "Refused to display document because display forbidden by X-Frame-Options". The workaround to this can be to move the whole iframe HTML code into the script (eg. using .html() in jQuery).
使用指向外部域的 attr() 可能会在 Chrome 中触发类似这样的错误:“拒绝显示文档,因为X-Frame-Options禁止显示”。解决方法是将整个 iframe HTML 代码移动到脚本中(例如,在 jQuery 中使用 .html())。
Example:
例子:
var divMapLoaded = false;
$("#container").scroll(function() {
if ((!divMapLoaded) && ($("#map").position().left <= $("#map").width())) {
$("#map-iframe").html("<iframe id=\"map-iframe\" " +
"width=\"100%\" height=\"100%\" frameborder=\"0\" scrolling=\"no\" " +
"marginheight=\"0\" marginwidth=\"0\" " +
"src=\"http://www.google.it/maps?t=m&cid=0x3e589d98063177ab&ie=UTF8&iwloc=A&brcurrent=5,0,1&ll=41.123115,16.853177&spn=0.005617,0.009943&output=embed\"" +
"></iframe>");
divMapLoaded = true;
}