先回顾在html中是如何使用框架的:
Code
<html>
<frameset cols="66%,34%">
<frame src="chicken.html" name="chicken">
<frameset rows="100,*">
<frame src="pastasalad.html" name="pastasalad">
<frame src="jellocubes.html" name="jellocubes">
</frameset>
</frameset>
</html>现在就来看在框架中运用JavaScript的例子:
Code
<html>
<frameset rows="25%,*">
<frame src="frames_example_controls.html" name="control_fram" >
<frame src="blank.html" name="target_frame" >
</frameset>
</html>里面有两个页面,一个是frames_example_controls.html,一个是blank.html:
frames_example_controls.html
<html>
</head>
<body>
<a href="#" onClick="top.target_frame.document.writeln('Monkey do!<br>');">Monkey see</a>
</body>
</html>
top.target_frame.document.writeln("Monkey do!")这句中的 top 是系统内置的变量,翻译的不好,还是看原文:This will always refer to the top-most browser window. If you want to start at the top of the window hierarchy, use top.
最后是一个练习,它涉及到前面学到的内容。
答案如下:
frame代码
<html>
<head>
<title>day_3_homework</title>
</head>
<frameset rows="40%,*" frameborder="1px" border="1px" scrolling="no">
<frame src="day_3_homework_control.html" name="control_frame">
<frame src="day_3_homework_default.html" name="show_frame">
</frameset>
</html>
这个没什么可说的,里而有一点,就是怎么取消框架分隔线
上面的frame取名为control_frame,其代码是:
Code
<html>
<head>
<title>day_3_homework_control</title>
</head>
<body>
<a href="day_3_homework_default.html" target="show_frame"><img src="image/home.gif"></a>
<a href="#"onClick="parent.show_frame.document.bgColor='#00FF00';"><imgsrc="image/green.gif"></a>
<a href="#" onClick="parent.show_frame.document.bgColor='yellow';"><img src="image/yellow.gif"></a>
<a href="day_3_homework_brandme.html" target="show_frame"><img src="image/monkey.gif" name="brandme_img"></a>
</body>
</html>
这段代码里有两个学习点:
下面的frame的内容将有两个页面载入,通过点分别点击control_frame内的两个小图,而显示,其代码分别为:
show_frame内的两个页面的代码
默认页面的代码是:
<html>
<head>
<title>day_3_homework_default</title>
</head>
<body>
<h1>Your Homework Assignment</h1>
<p>Make this page work. If you click on the monkey in the right corner of the page, you'll go to the branding
page from last lesson's homework. If you hit the Home button, you'll come back here. The yellow and green
buttons change the background color of this page.<br><br>
Get these buttons to work, and then View Source to see what I did. </p>
</body>
</html>
-----------------
另一页面的代码是:
<html>
<head>
<title>day_3_homework_brandme</title>
<script language="javascript">
<!--
var image1="image/monkey.gif";
var image2="image/thau.gif";
var image3="image/sun.gif";
-->
</script>
</head>
<body>
<h1>Browser Configuration</h1>
<p>Brand your browser by moving your mouse over the image below until you find one you like. Then click on
the image.</p>
<a href="#" onMouseOver="temp=image1;image1=image2;image2=image3;image3=temp;
window.document.the_image.src=image1;"
onclick="parent.control_frame.document.brandme_img.src=image1;">
<img src="image/monkey.gif" name="the_image" border="0"></a>
</body>
</html>
上面的代码里有很多学习点,值得认真琢磨