javascript中的帧

时间:2016-04-28 10:16:47

标签: javascript

<FRAMESET COLS="*,*" ONLOAD="selectFrames()" ONUNLOAD="alert('stopped')"
<FRAME SRC = "frames/grey.htm" NAME="firstFrame">


</FRAMESET>
<script language="javascript">

function selectFrames(){
base="frame/"
newFrames = new Array("red.htm","blue.htm","pink.htm","grey.htm")
window.firstFrame.location = base+newFrames[Math.round(5*Math.random())%5] 
window.secondFrame.location = base+newFrames[Math.round(5*Math.random())%5] 
}

它是一个简单的灰色设置框架buti不知道为什么代码不起作用。控制台错误设置属性位置未定义任何人都可以指导我关于现有代码的位置。

2 个答案:

答案 0 :(得分:1)

<FRAMESET COLS="*,*" ONLOAD="selectFrames()" ONUNLOAD="alert('stopped')">
  <FRAME SRC = "frames/grey.htm" NAME="firstFrame">
  <FRAME SRC = "frames/blue.htm" NAME="secondFrame">
</FRAMESET>
<script>
function selectFrames(){
    var bs = "frames/",
    newFrames = ["red.htm","blue.htm","pink.htm","grey.htm"];
    window.frames['firstFrame'].location.href = bs + newFrames[Math.round(Math.random()*5)];
    window.frames['secondFrame'].location.href = bs + newFrames[Math.round(Math.random()*5)];
}
</script>

要跟踪语法错误,请在头脑中添加此代码:

<head><script>
"use strict";
window.onerror = function(msg, url, line){
    alert(unescape(msg) + '\nFile: <a href="' + url + '">' + url + '</a>\nat Line: ' + line);
}
</script></head>

答案 1 :(得分:0)

您错过了<FRAMESET代码上的结束括号:

<FRAMESET COLS="*,*" ONLOAD="selectFrames()" ONUNLOAD="alert('stopped')"

应该是:

<FRAMESET COLS="*,*" ONLOAD="selectFrames()" ONUNLOAD="alert('stopped')">

您还在javascript中从行尾删除了分号。