您的位置:首页 > Web前端 > JavaScript

一起来进行javascript全面系统再学习(正在学习,不断更新中。。。),(2)JavaScript 对象 .

2011-12-21 17:09 525 查看
1,javascript内置对象,注意:对象只是一种特殊的数据。对象拥有属性和方法。

JS字符串
计算字符串长度:<scripttype="text/javascript">vartxt="HelloWorld!"document.write(txt.
length
)</script>

输出大写字符串:<scripttype="text/javascript">varstr="Helloworld!"document.write(str.
toUpperCase()
)</script>

字符串关于各种样式的设置:

<html>
<body>

<scripttype="text/javascript">

vartxt="HelloWorld!"

document.write("<p>Big:"+txt.big()+"</p>")
document.write("<p>Small:"+txt.small()+"</p>")

document.write("<p>Bold:"+txt.bold()+"</p>")
document.write("<p>Italic:"+txt.italics()+"</p>")

document.write("<p>Blink:"+txt.blink()+"(doesnotworkinIE)</p>")
document.write("<p>Fixed:"+txt.fixed()+"</p>")
document.write("<p>Strike:"+txt.strike()+"</p>")

document.write("<p>Fontcolor:"+txt.fontcolor("Red")+"</p>")
document.write("<p>Fontsize:"+txt.fontsize(16)+"</p>")

document.write("<p>Lowercase:"+txt.toLowerCase()+"</p>")
document.write("<p>Uppercase:"+txt.toUpperCase()+"</p>")

document.write("<p>Subscript:"+txt.sub()+"</p>")
document.write("<p>Superscript:"+txt.sup()+"</p>")

document.write("<p>Link:"+txt.link("http://www.w3school.com.cn")+"</p>")
</script>

</body>
</html>
indexOf()方法:
<scripttype="text/javascript">

varstr="Helloworld!"
document.write(str.indexOf("Hello")+"<br/>")
document.write(str.indexOf("World")+"<br/>")
document.write(str.indexOf("world"))

</script>


match()方法:

<scripttype="text/javascript">

varstr="Helloworld!"
document.write(str.match("world")+"<br/>")
document.write(str.match("World")+"<br/>")
document.write(str.match("worlld")+"<br/>")
document.write(str.match("world!"))
</script>

replace()方法:

<body>

<scripttype="text/javascript">

varstr="VisitMicrosoft!"
document.write(str.replace(/Microsoft/,"W3School"))

</script>
</body>

JS日期
JavaScriptDate(日期)对象:

返回当日的日期和时间:如何使用Date()方法获得当日的日期。代码:document.write(Date())结果是WedDec2116:37:272011

getTime():getTime()返回从1970年1月1日至今的毫秒数。

setFullYear():如何使用setFullYear()设置具体的日期。

代码:

vard=newDate()

d.setFullYear(1992,10,3)

document.write(d)

结果是:TueNov316:41:21UTC+08001992

toUTCString():如何使用toUTCString()将当日的日期(根据UTC)转换为字符串。

getDay():如何使用getDay()和数组来显示星期,而不仅仅是数字。

<scripttype="text/javascript">

vard=newDate()
varweekday=newArray(7)
weekday[0]="星期日"
weekday[1]="星期一"
weekday[2]="星期二"
weekday[3]="星期三"
weekday[4]="星期四"
weekday[5]="星期五"
weekday[6]="星期六"

document.write("今天是"+weekday[d.getDay()])

</script>


结果是:今天是星期三

显示一个钟表:如何在网页上显示一个钟表。

<html>
<head>
<scripttype="text/javascript">
functionstartTime(){
vartoday=newDate()//得到当前时间对象
varh=today.getHours()//现在的小时数
varm=today.getMinutes()//现在的分钟数
vars=today.getSeconds()//现在的描述
//addazeroinfrontofnumbers<10--将小于10的数值钱前加上0
m=checkTime(m)//调用下面的方法--作用:将12:3:4变成12:03:04
s=checkTime(s)
document.getElementById('txt').innerHTML=h+":"+m+":"+s//在<divid="txt"></div>上显示时间
t=setTimeout('startTime()',500)//js的一个计时器..setTimeOut()两个参数1:要执行的方法..2:间隔的时间(毫秒为单位)1000ms=1s
}

functioncheckTime(i){//将小于10的数值前面加上0
if(i<10){
i="0"+i
}
returni
}
</script>
</head>

<bodyonload="startTime()"><!--该文件加载时执行startTime()方法-->
<divid="txt"></div><!--显示时间的位置-->
</body>
</html>

日期间的比较:

日期对象也可用于比较两个日期。

下面的代码将当前日期与2008年8月9日做了比较:

varmyDate=newDate();
myDate.setFullYear(2008,7,9);

vartoday=newDate();

if(myDate>today)
{
alert("Todayisbefore9thAugust2008");
}
else
{
alert("Todayisafter9thAugust2008");
}

JS数组
数组对象的作用是:使用单独的变量名来存储一系列的值

(1)创建

<html><body><scripttype="text/javascript">
varmycars=newArray()
mycars[0]="Saab"
mycars[1]="Volvo"
mycars[2]="BMW"

for(i=0;i<mycars.length;i++)
{
document.write(mycars[i]+"<br/>")
}
</script></body></html>

(2)for---in用法

<html>
<body>
<scripttype="text/javascript">
varx
varmycars=newArray()
mycars[0]="Saab"
mycars[1]="Volvo"
mycars[2]="BMW"

for(xinmycars)
{
document.write(mycars[x]+"<br/>")
}
</script>
</body>
</html>

(3)合并数组,concat方法。

<html><body><scripttype="text/javascript">

vararr=newArray(3)
arr[0]="George"
arr[1]="John"
arr[2]="Thomas"

vararr2=newArray(3)
arr2[0]="James"
arr2[1]="Adrew"
arr2[2]="Martin"
alert(arr)
document.write(arr.concat(arr2)+'。')

</script></body></html>


(4)连接数组。所有元素组成一个字符串。

<html><body>
<scripttype="text/javascript">
vararr=newArray(3);
arr[0]="George"
arr[1]="John"
arr[2]="Thomas"
document.write(arr.join());
document.write("<br/>");
document.write(arr.join("."));
</script></body></html>

结果是:

George,John,Thomas

George.John.Thomas

(5)排序。

以下方法从字面上对数组进行排序。

<html><body><scripttype="text/javascript">

vararr=newArray(6)
arr[0]="George"
arr[1]="John"
arr[2]="Thomas"
arr[3]="James"
arr[4]="Adrew"
arr[5]="Martin"

document.write(arr+"<br/>")
document.write(arr.sort())

</script></body></html>

以下sort()方法从数值上对数组进行排序。

<html>
<body>

<scripttype="text/javascript">

functionsortNumber(a,b)
{
returna-b
}

vararr=newArray(6)
arr[0]="10"
arr[1]="5"
arr[2]="40"
arr[3]="25"
arr[4]="1000"
arr[5]="1"

document.write(arr+"<br/>")
document.write(arr.sort(sortNumber))

</script>

</body>
</html>


结果是:

10,5,40,25,1000,1

1,5,10,25,40,1000

如果想要从大到小排列的话,应该采用returnb-a

方法描述FFIE
concat()连接两个或更多的数组,并返回结果。14
join()把数组的所有元素放入一个字符串。元素通过指定的分隔符进行分隔。14
pop()删除并返回数组的最后一个元素15.5
push()向数组的末尾添加一个或更多元素,并返回新的长度。15.5
reverse()颠倒数组中元素的顺序。14
shift()删除并返回数组的第一个元素15.5
slice()从某个已有的数组返回选定的元素14
sort()对数组的元素进行排序14
splice()删除元素,并向数组添加新元素。15.5
toSource()返回该对象的源代码。1-
toString()把数组转换为字符串,并返回结果。14
toLocaleString()把数组转换为本地数组,并返回结果。14
unshift()向数组的开头添加一个或更多元素,并返回新的长度。16
valueOf()返回数组对象的原始值14
JS逻辑
Boolean(逻辑)对象用于将非逻辑值转换为逻辑值(true或者false)。

<html><body><scripttype="text/javascript">
varb1=newBoolean(0)
varb2=newBoolean(1)
varb3=newBoolean("")
varb4=newBoolean(null)
varb5=newBoolean(NaN)
varb6=newBoolean("false")

document.write("0是逻辑的"+b1+"<br/>")
document.write("1是逻辑的"+b2+"<br/>")
document.write("空字符串是逻辑的"+b3+"<br/>")
document.write("null是逻辑的"+b4+"<br/>")
document.write("NaN是逻辑的"+b5+"<br/>")
document.write("字符串'false'是逻辑的"+b6+"<br/>")
</script></body></html>

结果是:

0是逻辑的false

1是逻辑的true

空字符串是逻辑的false

null是逻辑的false

NaN是逻辑的false

字符串'false'是逻辑的true

注意:如果省略value参数,或者设置为0、-0、null、""、false、undefined或NaN,则该对象设置为false。否则设置为true(即使value参数是字符串"false")。

JS算数
<scripttype="text/javascript">

document.write(Math.random())
document.write(Math.max(5,7)+"<br/>")
document.write(Math.min(5,7)+"<br/>")

</script>
<html><body><scripttype="text/javascript">
document.write(Math.round(0.60)+"<br/>")
document.write(Math.round(0.50)+"<br/>")
document.write(Math.round(0.49)+"<br/>")
document.write(Math.round(-4.40)+"<br/>")
document.write(Math.round(-4.60))
</script></body></html>

math对象:Math对象并不像Date和String那样是对象的类,因此没有构造函数Math(),像Math.sin()这样的函数只是函数,不是某个对象的方法。您无需创建它,通过把Math作为对象使用就可以调用其所有属性和方法。

JSRegExp
RegExp是正则表达式的缩写。当您检索某个文本时,可以使用一种模式来描述要检索的内容。RegExp就是这种模式。

RegExp对象有3个方法:test()、exec()以及compile()。

1,test()方法:返回true或者false

<body><scripttype="text/javascript">
varpatt1=newRegExp("e");
document.write(patt1.test("Thebestthingsinlifearefree"));
</script></body>

2,exec()方法:检索字符串中的指定值。返回值是被找到的值。如果没有发现匹配,则返回null。
<html><body><scripttype="text/javascript">
varpatt1=newRegExp("e");

document.write(patt1.exec("Thebestthingsinlifearefree"));
</script></body></html>
加入参数:
<html><body>
<scripttype="text/javascript">
varpatt1=newRegExp("e","g");
do
{
result=patt1.exec("Thebestthingsinlifearefree");
document.write(result);
}
while(result!=null)
</script></body></html>
结果:由于这个字符串中6个"e"字母,代码的输出将是:

eeeeeenull



3,compile()方法:

compile()方法用于改变RegExp。

compile()既可以改变检索模式,也可以添加或删除第二个参数。

<html><body>
<scripttype="text/javascript">
varpatt1=newRegExp("e");
document.write(patt1.test("Thebestthingsinlifearefree"));
patt1.compile("d");
document.write(patt1.test("Thebestthingsinlifearefree"));
</script></body></html>

结果是:truefalse

参考手册:http://www.w3school.com.cn/js/jsref_obj_regexp.asp

JSHTMLDOM
除了内置的JavaScript对象以外,你还可以使用JavaScript访问并处理所有的HTMLDOM对象。

http://www.w3school.com.cn/js/js_obj_htmldom.asp
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: