【发布时间】:2014-09-08 20:07:58
【问题描述】:
过去几个小时我一直在尝试更改 iFrame 的 src。这似乎是一项简单的任务,但由于某种原因,经过数百次尝试并阅读了堆栈上的许多其他问题和答案,我仍然发现自己无法更改 iFrame 的 src。正如您从下面的代码中看到的那样,我已经尝试了很多次。我做错了什么?
<body>
<p><iframe src="http://localhost/ok.html" name="myFrame" width="500" marginwidth="0" height="500" marginheight="0" align="middle" scrolling="auto"></iframe>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
function loadPages(){
var loc = "http://localhost/morningmarketweb/index.html";
//var myIframe = document.getElementById("myFrame");
//window.frames[myIframe].location = "http://localhost/morningmarketweb/index.html";
// $(myIframe).load('http://localhost/morningmarketweb/index.html');
// document.getElementId('myFrame').src="google.com";
// var myIframe = document.getElementById('myFrame');
// myIframe.src=loc;
//$('#myFrame').attr('src', loc);
document.getElementById('myFrame').setAttribute('src', loc);
}
</script>
</body>
【问题讨论】:
-
你的
iframe中没有id属性myFrame -
@Garis & Ben:如果我问“我做错了什么”,这不能重复。如果它是重复的,那么你是在说这些其他人以某种方式提前知道,深入未来,我会问我会做错什么。我认为这是不可能的。
-
拉希尔,没错。我一直在看名字。我检查了一百万次,但从未发现 id="myFrame" 丢失。
标签: javascript jquery iframe