JavaScript是面向对象的语言(OOP)
JavaScript的对象有:JS String,JS Date,JS Array,JS Boolean,JS Math,JS Number,JS RegExp,JS Function,JS Events
1.JavaScript字符串string对象
(1)创建一个string字符串对象
new String(s) 或 String(s);
(2)String对象的属性 length
<html>
<body>
<script type="text/javascript">
var x="Hello World!";
document.write(x.length+"<br/>");
</script>
</body>
</html>
(3)String对象的方法
big() | 用大号字体显示字符串 |
small() | 用小号字体显示字符串 |
bold() | 用粗体显示字符串 |
blink() | 在IE浏览器下用闪烁字体显示字符串 |
italics() | 用斜体显示字符串 |
strike() | 用删除线来显示字符串 |
sub() | 把字符串显示为下标 |
sup() | 把字符串显示为上标 |
toLowerCase() | 把字符串转化为小写 |
toUpperCase() | 把字符串转化为大写 |
fontcolor() | 使用指定的颜色来显示字符串 |
fontsize() | 使用指定的大小来显示字符串 |
link() | 将字符串显示为链接 |
charAt() | 返回指定位置的字符 |
charCodeAt() | 返回指定位置字符的Unicode编码 |
indexOf() | 检索字符串 |
lastIndexOf() | 从后向前搜索字符串 |
match() | 找到一个或多个表达式的匹配 |
concat() | 链接字符串 |
replace() | 替换与正则表达式匹配的字符串 |
split() | 把字符串分割为字符串数组 |
substr() | 截取从起始字符串开始到指定位置的字符串 |
subString() | 截取指定起始位置的字符串 |
toString() | valueOf() |
(4)例子
<html>
<body>
<script type="text/javascript">
var txt="Hello World";
document.write("<p>Big:"+txt.big()+"</p>");
document.write("<p>Small:"+txt.small()+"</p>");
document.write("<p>Bold:"+txt.bold()+"</p>");
document.write("<p>Blink:"+txt.blink()+"</p>");
document.write("<p>Italic:"+txt.italics()+"</p>");
document.write("<p>Sub:"+txt.sub()+"</p>");
document.write("<p>Sup:"+txt.sup()+"</p>");
document.write("<p>FontColor:"+txt.fontcolor("Red")+"</p>");
document.write("<p>FontSize:"+txt.fontsize(16)+"</p>");
document.write("<p>toLowerCase:"+txt.toLowerCase()+"</p>");
document.write("<p>toUpperCase:"+txt.toUpperCase()+"</p>");
document.write("<p>CharAt:"+txt.charAt(2)+"</p>");
document.write("<p>CharCodeAt:"+txt.charCodeAt(2)+"</p>");
document.write("<p>IndexOf:"+txt.indexOf("World")+"</p>");
document.write("<p>IndexOf:"+txt.indexOf("worlld")+"</p>");
document.write("<p>LastIndexOf:"+txt.lastIndexOf("Hello")+"</p>");
document.write("<p>match:"+txt.match("Hello")+"</p>");
document.write("<p>match:"+txt.match("Helelo")+"</p>");
document.write("<p>Concat:"+txt.concat("rose rose")+"</p>");
document.write("<p>Replace:"+txt.replace(/World/,"yesterday")+"</p>");
document.write("<p>Split:"+txt.split(' ')+"</p>");
</script>
</body>
</html>
2.JavaScript 日期Date对象
(1)创建Date对象
var date = new Date();
(2)Date对象的方法
Date() | 返回当日的日期和时间 |
getDate() | 从Date对象返回一个月中的某一天(1-31) |
getDay() | 从Date对象返回一周中的某一天(0-6) |
getMonth() | 从Date对象返回月份(0-11) |
getFullYear() | 从Date对象以四位数字返回年份 |
getHours() | 返回Date对象的小时(0-23) |
getMinutes() | 返回Date对象的分钟(0-59) |
getSeconds() | 返回Date对象的秒数(0-59) |
getMilliSecond() | 返回Date对象的毫秒数(0-999) |
getTime() | 返回从1970年1月1日至今的毫秒数 |
setDate() | 设置Date对象中的某一天(1-31) |
setMonth() | 设置Date对象的月份(0-11) |
setFullYear() | 设置Date对象的年份 |
setHourse() | 设置Date对象的小时(0-23) |
setMinutes() | 设置Date对象的分钟(0-59) |
setSeconds() | 设置Date对象的秒数(0-59) |
setMilliSeconds() | 设置Date对象的毫秒数(0-999) |
setTime() | 以毫秒设置Date对象 |
toString() | 把Date转成字符串 |
toTimeString() | 把Date的时间部分转成字符串 |
toDateString() | 把Date的日期部门转成字符串 |
toLocaleString() | |
toLocaleTimeString() | |
toLocaleDateString() | |
(3)例子
<html>
<body>
<script type="text/javascript">
var date = new Date();
document.write("<p>当前日期和日期为:"+date+"<p/>");
document.write("<p>从1970年1月1日至今的毫秒数为:"+date.getTime()+"<p/>");
document.write("<p>可设置时间为:"+date.setFullYear(1987,8,21)+"<p/>");
document.write("<p>可置世界时间格式为”"+date.toUTCString()+"<p/>")
var weekdays = new Array();
var today = new Date();
weekdays[1]="星期一";
weekdays[2]="星期二";
weekdays[3]="星期三";
weekdays[4]="星期四";
weekdays[5]="星期五";
weekdays[6]="星期六";
weekdays[0]="星期天";
document.write("<p>今天是"+weekdays[today.getDay()]+"<p/>");
if(today>date)
{
alert(date+"在当前日期前。");
}
else
{
alert(date+"在当前日期后。");
}
</script>
</body>
</html>
显示实时时间
<html>
<head>
<script type="text/javascript">
function timeNow()
{
var date = new Date();
var h = date.getHours();
var m = date.getMinutes();
var s = date.getSeconds();
m=checkNum(m);
s=checkNum(s);
document.getElementById("txt").innerText =h+":"+m+":"+s//取得ID为txt的对象,即<div ID="txt"></div>节点,在这个节点内写入h+":"+m+":"+s
t=setTimeout('timeNow()',500)
}
function checkNum(i)
{
if(i<10)
{
i="0"+i;
}
return i;
}
</script>
</head>
<body οnlοad="timeNow()">
<div ID="txt"></div>
</body>
</html>