哈,我来迟了哦,废话少说,代码地给你看:
<html>
<head>
<title>doing the Date Right</title>
</heda>
<body>
<script language="JavaScript">
//Array of day names
var dayNames=new Array();
dayNames[0]="Sunday";
dayNames[1]="Monday";
dayNames[2]="Tuesday";
dayNames[3]="Wednesday";
dayNames[4]="Thursday";
dayNames[5]="Friday";
dayNames[6]="Saturday";
//Array of month Names
var monthNames = new Array();
monthNames[0]="January";
monthNames[1]="February";
monthNames[2]="March";
monthNames[3]="April";
monthNames[4]="May";
monthNames[5]="June";
monthNames[6]="July";
monthNames[7]="August";
monthNames[8]="September";
monthNames[9]="October";
monthNames[10]="November";
monthNames[11]="December";
//Elements of Date object assigned to variables
var now = new Date();
var day = now.getDay();
var month = now.getMonth();
var year = now.getYear();
var date = now.getDate();
//code to print fully formatted date (e.g., Monday,July 30,2001)
document.write("<h1>" + dayNames[day] + "," + monthNames[month] + "");
document.write(date + "," + year + "</h1>");
</script>
</body>
</html>
选择菜单:
就像DW中的跳转菜单一样,我们要实现的就是这个功能:
分三步踏入社会主义:
1:为每个网页的URL和名字创建数组
2:在网页上显示表单
3:编写菜单中的“跳转”代码
我们还是老习惯,先看代码后解释(在你看完代码时先不要急着看解释,试着自己理解
一下):
<html>
<head>
<title>Jumpin Jive</title>
<script language="JavaScript">
//The pages array holds the descriptions of the pages
var pages = new Array(
"Select a Page",
"Autos",
"Business an Careers",
"Computing",
"Entertainment",
"Games");
//The urls array holds the URLs of the pages
var urls = new Array(
"",
"autos.html",
"business.html",
"computer.html",
"entertainment.html",
"games.html");
//The goPage() function determines which page is selected and goes to it
function goPage(form) {
var i = form.jumpMenu.selectedIndex;
if (i!=0) {
window.location=urls[i];
}
}
</script>
</head>
<body>
<p>Choose a Page and Jump:</p>
<script language="JavaScript">
//The select menu is displayed wherever you place this code
document.write(’<form>’);
document.write(’<select name="jumpMenu" onChange="goPage(this.form);">’);
for (var i=0;i<pages.length;i++) {
document.write(’<option>’ + pages[i]+’</option>’);
}
document.write(’</select>’);
document.write(’</form>’);
</script>
</body>
</html>
1:首先建立了两个相对应的数组pages和urls在pages数组里的每一个项都有一个对应的URL,pages中的第一个元素不会有跳转,因为他对应的是空“”。
2:在页面上显示,这里使用了一个for语句,在select标签中用到了一个onChange事件容器。for语句和其他程序语言一样,是一 种常用的程序设计结构,它重复运行一个指定的代码块,只到其条件被满足为止。
在上面的代码中它的作用就是不停的写出<optiong>XXX</option>直到i小于pages这个数组长度的最大整数为止!
3:goPage()函数:
次函数用到了选择菜单的selectedIndex属性判断所选顶的选项。
例如,如果选定了第一个悬想,selectedIndex的值就为0。第二项就为1。。。函数用这些信息来决定要用的URL。
既然select a page什么都不做,那么就告诉selectedIndex为0,函数就不需要改变网页。只有当i不等于零时函数才继续往下执行。
4:引申:如何在框架中使用:parent.framename.location=urls[i];
好了,今天的课就到这里,下课了,放羊了!!!!!!!!玩泡泡去!!!
|