站内搜索: 请输入搜索关键词

当前页面: 开发资料首页Javascript 专题JavaScript类和内置对象

JavaScript类和内置对象

摘要: JavaScript类和内置对象
<tr> <td>

Javascript 对象

Javascript是基于对象而非面向对象的程序设计语言,它不支持类和继承,Javascript在面向对象方面与Java的严谨相比,显得非常随便。


创建对象


Javascript通
过实现包含属性说明和方法定义的构造方法来声明类,并没有专门的class类定义。

如:用来声明Oven
类的构造方法

function Oven(color,type,time)

{this.color=color;

this.type=type;

this.time=time;

this.info=info;

}

function info()

{document.writeln("
颜色:",this.color);

document.writeln("
类型:",this.type);

document.writeln("出厂日期:",this.time);

}

创建Oven类对象实例:

oven1=new Oven("
红色","精巧型","99.1.1");

oven2=new Oven("
蓝色","典雅型","99.10.1");

oven3=new Oven("黄色","
实用型","2000.1.1");

jscript1.html:



<head>

"</SPAN>欢迎选购"



function Oven(color,type,time)

{this.color=color;

this.type=type;

this.time=time;

this.info=info;

}

function info()

{document.writeln("颜色:",this.color,"
");

document.writeln("
类型:",this.type,"
");

document.writeln("出厂日期:",this.time,"
");

}

</script>

</head>

<body>

烤箱商品



<script>

oven1=new Oven("红色","精巧型","1999年1月1日");

oven2=new Oven("
蓝色","典雅型","1999年10月1日");

oven3=new Oven("黄色","
实用型","2000年1月1日");

</script >






oven1.info();

</script>




<script>

oven2.info();

</script>




<script>

oven3.info();

</script>




</body>




Javascript
还可通过下列方法方便地创建一个对象且可随意添加属性:

var oven1=new Object();

oven1.color="c1";

oven1.type="t1";

oven1.time="t1";


Javascript的可用
对象有:


anchor

applet

button

checkbox

Date

document

form

history

link

location

Math

password

radioButton

reset

selection

string

submit

text

textArea

window


Navigator的
对象继承树


navigator

window


history

document


anchors

forms

links

location


一:window对象


window
对象是 Javascript的最高层对象,它包含除了Navigator对象外的所有对象。


注:有些方法和属性只有支持Javascript1.2的
浏览器才能使用。


1.属性:


defaultStatus:
设置状态行默认字符串,只读。


status:描述状
态行字符串,可读写。


frames:包含窗口中所有FRAME的
对象数组,frames.length可取得FRAME个数。


frame:一个FRAME
对象。


self:当前窗口。


parent:父窗口或frameset。


top:当前窗口的最高
层窗口。


name:窗口的名字。


innerHeight:
给出浏览器窗口内部高度。


innerWidth:
给出浏览器窗口内部宽度。


outerHeight:
给出整个浏览器窗口高度。


outerWidth:
给出整个浏览器窗口宽度。


locationbar:
设置显示或隐藏浏览器地址栏,如:locationbar=true


menubar:
设置显示或隐藏浏览器菜单栏,如:menubar=false


personalbar:
设置显示或隐藏浏览器指示栏,如:personalbar=false


scrollbars:
设置显示或隐藏窗口滚动条,如:scrollbars=true


statusbar:
设置显示或隐藏浏览器状态栏,如:statusbar=true


toolbar:
设置显示或隐藏浏览器工具栏,如:toolbar=false


java:提供
访问JAVA API类属性和方法,如:java.lang.Math.random。


netscape:提供引用netscape.*工具包。


sun:提供引用sun.*工具包。


document:窗口中当前所包含的文档
对象。


location:指定当前文档URL的字符串。


history:与窗口
关联的history对象。


2. 方法


alert("string"):打
开一个警告对话框并显示string信息。


confirm("string"):打
开一个带有OK和Cancel按钮的对话框并显示string信息,若用户选择OK返回true,选择Cancel返回false。


prompt("string"): 打开一个显示string信息并带有单行文本输入域的对话框,对话框关闭时返回用户输入的字符串。(见窗口举例1)


setTimeout:以毫秒
为单位为一将要发生事件设置时间。(见窗口举例2和3)


clearTimeout:重置setTimeout所作的
设置。


captureEvent(eventType):
设置窗口捕捉指定类型的事件。


releaseEvents(eventType):停止捕捉指定
类型事件。


handleEvent(event):若
设置了captureEvent(),则指定类型的事件将传递给handleEvent()方法。


focus():使窗口得到输入焦点。


blur():除去当前窗口中的
输入焦点。


stop():停止当前文件的下
载,相当按下“Stop”按钮。


close():
关闭窗口。


open(url,name,features,replaceFlag):
查找一窗口或打开一新窗口。(见窗口举例2)


url:指定窗口中
显示的资源,空字符串将打开一空白窗口。


name:是窗口的名字。


replaceFlag:
值为true时,新窗口将取代原窗口在历史记录中的位置。


features:
给出逗号分隔窗口属性值,如:menubar=no。


features:


alwaysLowered=yes|no:窗口是否
总在其它窗口下面。


alwaysRaised=yes|no:窗口是否
总在其它窗口上方。


dependent=yes|no:窗口是否依附于父窗口,即父窗口
关闭时它也关闭。


hotkeys=yes|no:是否取消
热键。


location=yes|no:是否
显示当前URL。


menubar=yes|no:是否
显示菜单栏。


resizable=yes|no:是否允
许用户改变窗口大小。


scrollbars=yes|no:是否
带滚动条。


status=yes|no:是否
显示窗口底部的状态栏。


titlebar=yes|no:是否
显示标题栏。


toolbar=yes|no:是否
显示工具栏。


z-lock=yes|no:是否固定窗口位置。


height|innerHeight=象素
值:窗口内容区高度。


width|innerWidth=象素值:窗口内容区宽度。


outerHeight=象素
值:窗口高度。


outerWidth=象素值:窗口宽度。


screenX=象素
值:窗口左边框位置。


screenY=象素
值:窗口上边框位置。


3.窗口事件


onBlur():窗口失去焦点
时。


onDragdrop():当文件或快捷方式被拖
动到窗口时。


onError:当出
现Javascript错误时。


onFocus():窗口
获得焦点时。


onLoad():
浏览器装入页面时。


onMove():窗口被移
动时。


onResize():窗口被改
变大小时。


onUnload():用
户离开页面时。


4.窗口
举例


例1: promptdemo.html




<head>

javascript prompt demo

</head>

<body>

<script Language="Javascript">

document.write("

prompt demo

");

msg= prompt ("请输入内容:");

document.write(msg+"

");

document.close();

</script>

</body>




例2: windowDemo.html


<script language="Javascript">

function newwindow()

{ window. open ("../script1.htm","localwindow","toolbar=no,statusbar=no,menubar=no,


scrollbars=yes,resizable=no,width=200,height=200");


}


window. status ="我只待一会儿!";

setTimeout ("erase()",3000);


function erase()

{

window.status="";

}

</script>



<head>

Javascript window Demo

</head>

<body onLoad="newwindow()">

这是窗口大小使用演示,请注意看一下状态栏!




</body>




例二: windowdemo2.html




function scroll(seed)

{var msg="
欢迎来到袁方JAVA讲坛,讲坛网址:www.triworks.com.cn";

var out = " ";

var c = 1;

if (seed > 100)

{seed--;

var cmd="scroll(" + seed + ")";

timerTwo=window.setTimeout(cmd,100);

}

else

if (seed <= 100 && seed > 0)

{for (c=0 ; c < seed ; c++)

{out+=" ";

}

out+=msg;

seed--;

var cmd="scroll(" + seed + ")";

window.status=out;

timerTwo=window.setTimeout(cmd,100);

}

else

if (seed <= 0)

{if (-seed < msg.length)

{out+=msg.substring(-seed,msg.length);

seed--;

var cmd="scroll(" + seed + ")";

window.status=out;

timerTwo=window.setTimeout(cmd,100);

}

else

{window.status=" ";

timerTwo=window.setTimeout("scroll(100)",7);

}

}

}

timerONE=window.setTimeout('scroll(100)',50);

</script>



<head>

Javascript window Demo2

</head>

<body>






这是"跑马灯",↑返回目录
前一篇: JavaScript判断数字的合法性
后一篇: 设置可以把select 设成可以输入数据