javascript入门笔记(一)

[color=violet]http://www.bookofjavascript.com[/color]
[color=brown]javascript Editor下载:http://hnpy.onlinedown.net/soft/5463.htm [/color]
第一课: :oops:
javascript代码放在[color=orange]<script type="text/javascript"></script>[/color]之间,javascript标签可放在[color=orange]<head></head>[/color]之间,便于查找。

第二课:使用变量和内置函数自动更新网页 8)
1.[color=red]window.document.write("hello"); [/color]//通过write()向网页写入内容。
2.[color=red]alert("Pay attention!"); [/color]//警告
3.[color=red]var the_name= prompt("Enter your name","Enter your name here") [/color]//填写信息并显示在页面上。
[color=red] window.document.write(the_name);[/color]
4.向网页写入日期:[color=red]var now = new Date(); [/color]//内置日期对象,日期对象有很多方法:
[color=green] getDate() 以整数1-31表示每月中的天。
getDay() 以整数表示星期几,其中0表示星期天,1表示星期一
getHours() 以整数0-23表示小时
getMinutes() 以整数0-59表示分钟
getSeconds() 以整数0-59表示秒
getMonth() 以整数0-11表示月份
getTime() 以毫秒表示当前时间,其中0表示1970年1月1日00:00:00
getYear() 年份,在各个浏览器之间各不相同。[/color]
写入日期和时间的代码: :arrow:
[color=blue]<html>
<head>
<title> *** Date*** </title>
<script type="text/javascript">
var now = new Date();
var the_year=now.getYear();
var the_month=now.getMonth();
var the_date=now.getDate();
var the_hour=now.getHours();
var the_minute=now.getMinutes();

the_month=the_month+1; //月份+1,因为getMonth()默认一月份是0
the_year=fixY2k(the_year); //Mac平台得Netscape4.0总是返回当前年份减去1900的值,用fixY2k可解决这个bug.
the_minute=fixTime(the_minute);

var date_string = the_year+"/"+the_month+"/"+the_date;
var time_string = the_hour+":"+the_minute;
var date_time_string="Today is "+date_string+".The time is "+time_string+".";

function fixY2k(number){
if(number<1000){
number=number+1900;
}
return number;
}
function fixTime(number){
if (number<10){
number="0"+number;
}
return number;
}
</script>
</head>

<body bgcolor="white">
<script type="text/javascript">
document.write(date_time_string);
</script>
</body>
</html>[/color]
http://www.esa.int欧洲航天局的主页中显示了当前的日期。
代码如下:
[color=blue]<script type="text/javascript">
var now = new Date();
var yyyy=now.getFullYear();
var mm=now.getMonth()+1;
if(10>mm) mm="0"+mm;
var dd = now.getDate();
if(10>dd) dd="0"+dd;
document.write(dd+'-'+mm+'-'+yyyy);
</script>[/color]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值