JavaScript的对象

转载 2004年08月20日 15:24:00

The following objects are available in JavaScript:

anchor applet button checkbox Date document form history link location Math password radioButton reset selection string submit text textArea window

NOTE: Each object topic indicates whether the object is part of the client (in Navigator), server (in LiveWire), or is common (built-in to JavaScript). Server objects are not included in this version of the documentation.


anchor object (client)

An anchor is a piece of text identified as the target of a hypertext link.

Syntax

To define an anchor, use standard HTML syntax :

<A NAME="anchorName"
   anchorText
</A>

NAME specifies a tag that becomes an available hypertext target within the current document.
anchorText specifies the text to display at the anchor.

Description

You can reference the anchor objects in your code by using the anchors property of the document object. The anchors property is an array that contains an entry for each anchor in a document.

xxx to be supplied

Properties

xxx to be supplied

Methods

xxx to be supplied

Event handlers

None.

Examples

<A NAME="javascript_intro"><H2>Welcome to JavaScript</H2></A>

See also

link object anchors property

applet object (client)

Represents a Java applet. xxx NYI.

Syntax

xxx to be supplied

Description

The applet object executes applets written in Java, not JavaScript. Java applets execute only on Netscape 2.0 and HotJava, and only on 32-bit platforms, such as Windows 95, Windows NT, and Unix. They do not execute on the 16-bit Windows platforms, such as Windows 3.1.

Properties

length

Methods

xxx to be supplied

Event handlers

xxx to be supplied

Examples

xxx to be supplied

See also

xxx to be supplied

button object (client)

A button object is a pushbutton on an HTML form.

Syntax

<INPUT
   TYPE="button"
   NAME="objectName"
   VALUE="buttonText"
   [onClick="handlerText"]>
NAME specifies the name of the button object as a property of the enclosing form object and can be accessed using the name property. VALUE specifies the label to display on the button face and can be accessed using the value property.

Description

The button object is a custom button that you can use to perform an action you define.

Properties

name value

Methods

click

Event handlers

onClick

Examples

A custom button does not necessarily load a new page into the client; it merely executes the script specified by the onClick event handler. In the following example, myfunction() is a JavaScript function.

<INPUT TYPE="button" VALUE="Calculate" NAME="calc_button" onClick="myfunction(this.form)">

See also

reset and submit objects

checkbox object (client)

A checkbox object is a checkbox on an HTML form. A checkbox is a toggle switch that lets the user set a value on or off.

Syntax

To define a checkbox, use standard HTML syntax with the addition of the onClick event handler:

<INPUT
   TYPE="checkbox"
   NAME="objectName"
   [CHECKED]
   [onClick="handlerText"]>
   textToDisplay

Description

Use the checked property to specify whether the checkbox is currently checked. Use the defaultChecked property to specify whether the checkbox is checked when the form is loaded.

Properties

checked defaultChecked name value

Methods

click

Event handlers

onClick

Examples

<B>Specify your music preferences (check all that apply):</B> <BR><INPUT TYPE="checkbox" NAME="musicpref_rnb" CHECKED> R&B <BR><INPUT TYPE="checkbox" NAME="musicpref_jazz" CHECKED> Jazz <BR><INPUT TYPE="checkbox" NAME="musicpref_blues" CHECKED> Blues <BR><INPUT TYPE="checkbox" NAME="musicpref_newage" CHECKED> New Age

See also

xxx to be supplied

Date object (common)

JavaScript has a date object that enables you to work with dates and times. JavaScript handles dates very similar to the way Java handles dates: They have many of the same date methods, and both languages store dates internally as the number of milliseconds since January 1, 1970 00:00:00.

Syntax

To create a date object:

varName = new Date(parameters)
where varName is a JavaScript variable name for the date object being created; it can be a new object or a property of an existing object.

The parameters for the Date constructor can be any of the following:

  • Nothing: creates today's date and time. For example, today = new Date()
  • A string representing a date in the following form: "Month day, year hours:minutes:seconds". For example, Xmas95= new Date("December 25, 1995 13:30:00") If you omit hours, minutes, or seconds, the value will be set to zero.
  • A set of integer values for year, month, and day. For example, Xmas95 = new Date(95,11,25)
  • A set of values for year, month, day, hour, minute, and seconds For example, Xmas95 = new Date(95,11,25,9,30,0)

To use date methods:

dateObj.methodName(parameters)

Exceptions: The UTC and parse methods of date are static methods that you use as follows:

Date.UTC(params)
Date.parse(params)

Description

JavaScript does not have a date data type. However, the date object and its methods enable you to work with dates and times in your applications.

The date object has a large number of methods for setting, getting, and manipulating dates.

Properties

None.

Methods

getDate getDay getHours getMinutes getMonth getSeconds getTime getTimeZoneoffset getYear parse setDate setHours setMinutes setMonth setSeconds setYear toString toGMTString toLocaleString UTC

Event handlers

None. Built-in objects do not have event handlers.

Examples

xxx to be supplied

See also

xxx to be supplied

document object (client)

The document object contains information on the current document.

Syntax

To define a document object, use standard HTML syntax with the addition of the onLoad and onUnLoad event handlers:

<BODY
   BACKGROUND="backgroundImage"
   BGCOLOR="#backgroundColor"
   FGCOLOR="#foregroundColor"
   LINK="#unfollowedLinkColor"
   ALINK="#activatedLinkColor"
   VLINK="#followedLinkColor"
   [onLoad="handlerText"]
   [onUnLoad="handlerText"]>
</BODY>

BGCOLOR, FGCOLOR, LINK, ALINK, and VLINK are color names or color specifications in the format "#rrggbb".

Description

The <BODY>...</BODY> tag encloses an entire document, which is defined by the current URL. The entire body of the document (all other HTML elements for the document) goes within the <BODY>...</BODY> tag.

You can reference the anchors, forms, and links of a document by using the anchors, forms, and links properties. These properties are arrays that contain an entry for each anchor, form, or link in a document.

The document object's title property reflects the contents of <TITLE>...</TITLE>. Other properties reflect the contents of the document; for example, bgColor reflects the background color, and lastModified reflects the time last modified. Some of the properties are reflections from HTML attributes; for example, the links property is a reflection of all the links in the document, and the forms property is a reflection of all the forms in the document.

Properties

alinkColor anchors bgColor fgColor forms lastModified linkColor links loadedDate location referrer title vlinkColor

Methods

clear close open write writeln

Event handlers

onLoad onUnLoad

Examples

xxx to be supplied

See also

window object

form object (client)

A form lets users input text and make choices from form objects such as checkboxes, radio buttons, and selection lists. You can also use a form to post data to or retrieve data from a server.

Syntax

To define a form, use standard HTML syntax with the addition of the onSubmit event handler:

<FORM
   NAME="objectName
   TARGET="windowName"
   ACTION="serverURL"
   METHOD=GET | POST
   [onSubmit="handlerText"]>
</FORM>

TARGET specifies the window that form responses go to. When you submit a form with a TARGET attribute, instead of seeing the server's responses in the same window that contained the form, you see them in a (possibly new) window.
ACTION specifies the URL of the server to which form field input information is sent. METHOD specifies how information is sent to the server specified by ACTION. GET (the default) appends the input information to the URL which on most receiving systems becomes the value of the environment variable QUERY_STRING. POST sends the input information in a data body which is available on stdin with the data length set in the environment variable CONTENT_LENGTH.

Description

Each form in a document corresponds to a distinct object.

You can reference the form objects in your code by using the forms property of the document object. The forms property is an array that contains an entry for each form in a document.

You can reference a form's elements in your code by using the elements property. The elements property is an array that contains an entry for each element (such as a checkbox, radioButton, or text object) in a form.

Properties

action elements method name target

Methods

submit

Event handlers

onSubmit

Examples

xxx to be supplied

See also

elements, forms properties

history object (client)

The history object contains information on the URLs that the client has visited. This information is stored in a history list, and is accessible through the Navigator's Go menu.

Syntax

history.go(delta)
delta is an integer representing the offset of the destination URL in the history list.

Description

The history object is a linked list of URLs the user has visited, as shown in the Navigator's Go menu.

Properties

current length

Methods

back forward go

Event handlers

None.

Examples

The following example goes to the URL the user visited three clicks ago.

history.go(-3)

See also

xxx to be supplied

link object (client)

A link is a piece of text identified as a hypertext link. When the user clicks the link text, the link hypertext reference is loaded into its target window.

Syntax

To define a link, use standard HTML syntax with the addition of the onClick and onMouseOver event handlers:

<A HREF=locationOrURL
   TARGET="windowName"
   [onClick="handlerText"]
   [onMouseOver="handlerText"]>
   linkText
</A>

HREF identifies a destination anchor or URL.
TARGET specifies the window that the link is loaded into.
linkText is rendered as a hypertext link to the URL.

Description

Each link object is a location object.

You can reference the link objects in your code by using the links property of the document object. The links property is an array that contains an entry for each link in a document.

Properties

target

Methods

xxx to be supplied

Event handlers

onClick onMouseOver

Examples

The following example creates a hypertext link to an anchor named javascript_intro.

<A HREF="#javascript_intro" _fcksavedurl="#javascript_intro">Introduction to JavaScript</A>

The following example creates a hypertext link to a URL.

<A HREF="http://www.netscape.com" _fcksavedurl="http://www.netscape.com">Netscape Home Page</A>

See also

anchor object links property

location object (client)

The location object contains information on the current URL.

Syntax

location.property
property is one of the properties listed below.

Description

xxx to be supplied

Properties

hash host hostname href pathname port protocol search

Methods

assign toString

Event handlers

None.

Examples

xxx to be supplied

See also

xxx to be supplied

Math object (common)

The built-in Math object has properties and methods for mathematical constants and functions. For example, the Math object's PI property has the value of pi.

Syntax

Math.propertyName
or
Math.methodName(parameters)

Description

You reference the constant PI as Math.PI. Constants are defined with the full precision of real numbers in JavaScript.

Similarly, you reference Math functions as methods. For example, the sine function is Math.sin(argument), where argument is the argument.

It is often convenient to use the with statement when a section of code uses several Math constants and methods, so you don't have to type "Math" repeatedly. For example,

with Math {
   a = PI * r*r;
   y = r*sin(theta)
   x = r*cos(theta)
}

Properties

E LN10 LN2 PI SQRT1_2 SQRT2

Methods

abs acos asin atan ceil cos exp floor log max min pow random round sin sqrt tan

Event handlers

None. Built-in objects do not have event handlers.

Examples

xxx to be supplied

See also

xxx to be supplied

password object (client)

A password object is a text field on an HTML form. When the user enters text into the field, asterisks (*) hide anything entered from view.

Syntax

To define a password object, use standard HTML syntax:

<INPUT
   TYPE="password"
   NAME="objectName"
   [VALUE="textValue"]
   SIZE=integer

Description

xxx to be supplied

Properties

defaultValue name value

Methods

focus blur select

Event handlers

None.

Examples

 

<B>Password:</B> <INPUT TYPE="password" NAME="password" VALUE="" SIZE=25>

See also

xxx to be supplied

radioButton object (client)

A radioButton object is a set of radio buttons on an HTML form. A set of radio buttons lets the user choose one item from a list.

Syntax

To define a set of radio buttons, use standard HTML syntax with the addition of the onClick event handler:

<INPUT
   TYPE="radio"
   NAME="objectName"
   VALUE="buttonValue"
   [CHECKED]
   [onClick="handlerText"]>
   textToDisplay

NAME should contain the same value for all radio buttons in a group.

Description

All radio buttons in a radio button group use the same name property. To access the individual radio buttons in your code, follow the object name with an index starting from zero, one for each button the same way you would for an array such as forms: document.forms[0].objectName[0] is the first, document.forms[0].objectName[1] is the second, etc.

Properties

checked defaultChecked index length name value

Methods

click

Event handlers

onClick

Examples

The following example defines a radio button group to choose among three catalogs. Each radio button is given the same name, NAME="choice", forming a group of buttons for which only one choice can be selected. The example also defines a text field that defaults to what was chosen via the radio buttons but that allows the user to type a nonstandard catalog name as well. JavaScript automatically sets the catalog name input field based on the radio buttons.

<INPUT TYPE="text" NAME="catalog" SIZE="20"> <INPUT TYPE="radio" NAME="choice" VALUE="PRICEY" onClick="form.catalog.value = 'top-of-the-line'"> It's really great! <INPUT TYPE="radio" NAME="choice" VALUE="MODEST" onClick="form.catalog.value = 'middle-of-the-road'"> It's pretty good. <INPUT TYPE="radio" NAME="choice" VALUE="CHEAPO" onClick="form.catalog.value = 'bargain-basement'"> It stinks!

See also

selection object

reset object (client)

A reset object is a reset button on an HTML form.

Syntax

To define a reset button, use standard HTML syntax with the addition of the onClick event handler:

<INPUT
   [NAME="objectName"]
   TYPE="reset"
   VALUE="buttonText"
   [onClick="handlerText"]>
VALUE specifies the text to display on the button face and can be accessed using the value property.

Description

A reset button resets all elements in a form to their defaults.

Properties

name value

Methods

click

Event handlers

onClick

Examples

The following example displays a text object containing "CA". If the user types a different state abbreviation in the text object and then clicks the Clear Form button, the original value of "CA" is restored.

<B>State: </B><INPUT TYPE="text" NAME="state" VALUE="CA" SIZE="2"> <P><INPUT TYPE="reset" VALUE="Clear Form">

See also

button and submit objects

selection object (client)

A selection object is a selection list or scrolling list on an HTML form. A selection list lets the user choose one item from a list. A scrolling list lets the user choose one or more items from a list.

Syntax

To define a selection object, use standard HTML syntax with the addition of the onBlur, onChange, and onFocus event handlers:

<SELECT
   NAME="objectName"
   [SIZE="value"]
   [MULTIPLE]
   [onBlur="handlerText"]
   [onChange="handlerText"]
   [onFocus="handlerText"]>
   <OPTION [SELECTED]> textToDisplay [ ... <OPTION> textToDisplay]
</SELECT>

SIZE specifies the number of options visible when the form is displayed.

Description

You can reference the options of a selection object in your code by using the options property. The options property is an array that contains an entry for each option in a selection object. Each option has the properties listed below.

The options on selection objects can be updated dynamically. xxx NYI.

Properties

The selection object has the following properties:

options selectedIndex

The options property has the following properties:

defaultSelected index selected text value

Methods

click

Event handlers

onBlur onChange onFocus

Examples

The following example displays a selection list.

Choose the music type for your free CD: <SELECT NAME="music_type_single"> <OPTION SELECTED> R&B <OPTION> Jazz <OPTION> Blues <OPTION> New Age</SELECT> <P>Choose the music types for your free CDs: <BR><SELECT NAME="music_type_multi" MULTIPLE> <OPTION SELECTED> R&B <OPTION> Jazz <OPTION> Blues <OPTION> New Age</SELECT>

See also

radioButton object options property

string object (common)

A string object consists of a series of characters.

Syntax

stringName.property|method
stringName is the name of a string variable.
property is one of the properties listed below.
method is one of the methods listed below.

Description

A string can be represented as a literal enclosed by single or double quotes; for example, "Netscape" or 'Netscape'.

Properties

length

Methods

anchor big blink bold charAt fixed fontcolor fontsize indexOf italics lastIndexOf link small strike sub substring sup toLowerCase toUpperCase

Event handlers

None. Built-in objects do not have event handlers.

Examples

The following statement creates a string variable.

var last_name = "Schaefer"

last_name.length is 8.
last_name.toUpperCase() is "SCHAEFER".
last_name.toLowerCase() is "schaefer".

See also

text and textArea objects

submit object (client)

A submit object is a submit button on an HTML form.

Syntax

To define a submit button, use standard HTML syntax with the addition of the onClick event handler:

<INPUT
   TYPE="submit"
   NAME="objectName"
   VALUE="buttonText"
   [onClick="handlerText"]>
VALUE specifies the text to display on the button face and can be accessed using the value property.

Description

A submit button causes a form to be submitted.

Clicking a submit button submits a form to the program specified by the form's action property. This action always loads a new page into the client; it may be the same as the current page, if the action so specifies or is not specified.

Properties

name value

Methods

click

Event handlers

onClick

Examples

<INPUT TYPE="submit" NAME="submit_button" VALUE="Done">

See also

reset and button objects

text object (client)

A text object is a text input field on an HTML form. A text field lets the user enter a word, phrase, or series of numbers.

Syntax

To define a text object, use standard HTML syntax with the addition of the onBlur, on Change, onFocus, and onSelect event handlers:

<INPUT
   TYPE="text"
   NAME="objectName"
   VALUE="textValue"
   SIZE=integer
   [onBlur="handlerText"]
   [onChange="handlerText"]
   [onFocus="handlerText"]
   [onSelect="handlerText"]>

Description

text objects can be updated dynamically by setting this.value

xxx to be supplied

Properties

defaultValue name value

Methods

focus blur select

Event handlers

onBlur onChange onFocus onSelect

Examples

<B>Last name:</B> <INPUT TYPE="text" NAME="last_name" VALUE="" SIZE=25>

See also

textArea and string objects

textArea object (client)

A textArea object is a multiline input field on an HTML form. A textArea field lets the user enter words, phrases, or numbers.

Syntax

To define a text area, use standard HTML syntax with the addition of the onBlur, onChange, onFocus, and onSelect event handlers:

<TEXTAREA
   NAME="objectName"
   ROWS="integer"
   COLS="integer"
   [onBlur="handlerText"]
   [onChange="handlerText"]
   [onFocus="handlerText"]
   [onSelect="handlerText"]>
   textToDisplay
</TEXTAREA>

textToDisplay allows only ASCII text, and new lines are respected.
ROWS and COLS define the physical size of the displayed input field in numbers of characters.

Description

textArea objects can be updated dynamically. xxx need to explain how. (see text object)

Properties

defaultValue name value

Methods

focus blur select

Event handlers

onBlur onChange onFocus onSelect

Examples

<B>Description:</B> <BR><TEXTAREA NAME="item_description" ROWS=6 COLS=55> Our storage ottoman provides an attractive way to store lots of CDs and videos--and it's versatile enough to store other things as well. It can hold up to 72 CDs under the lid and 20 videos in the drawer below. </TEXTAREA>

See also

text and string objects

window object (client)

A window object is the top-level object for each document, location, and history object group.

Syntax

xxx to be supplied

Description

The window object is the top-level object in the JavaScript client hierarchy. Because the existence of the current window is assumed, you don't have to reference the name of the window when you call its methods and assign its properties. For example, status="Jump to a new location" is a valid property assignment, and close() is a valid method call.

The self and window properties are synonyms for the current window, and you can optionally use them to refer to the current window. For example, you can close the current window by calling either window.close() or self.close(). You can use these properties to make your code more readable, or to disambiguate the property assignment self.status from a form called status.

See the methods and properties listed below for more examples.

You can reference a window's frame objects in your code by using the frames property. The frames property is an array that contains an entry for each frame in a window.

Properties

frames parent self top status defaultStatus

Methods

alert close confirm open prompt setTimeout clearTimeout

Event handlers

onLoad onUnload

Examples

xxx to be supplied

See also

document object frames property

Javascript基于对象三大特征

Javascript基于对象三大特征 基本概述     Javascript基于对象的三大特征和C++,Java面向对象的三大特征一样,都是封装(encapsulation)、继承(inheri...
  • q547550831
  • q547550831
  • 2015年12月24日 14:46
  • 4242

JavaScript入门<2>JavaScript常用内置对象实例详解

1、什么是JavaScript对象? 2、如何使用JavaScript对象? 3、JavaScript有哪些常用内置对象? 一)String对象 二)String对象与正则表达式 1、什么是正则表达式...
  • xiu2016
  • xiu2016
  • 2016年10月16日 12:31
  • 782

JavaScript中的对象,如何创建对象,创建对象的7种模式

ECMA-262把对象定义为:”无需属性的集合,其属性可以包含基本值、对象或者函数。”严格来讲,这就相当于说明对象是一组没有特定顺序的值。对象的每个属性或方法都有一个名字,而每个名字都映射到一个值。正...
  • u014346301
  • u014346301
  • 2016年08月14日 16:33
  • 9378

javascript中的面向对象,你了解多少?

Javascript 是面向对象开发的脚本语言,既然javascript是面向对象开发的,那么面向对象的前提:抽象,应该必须有,那么看看这个对象的创建,该如何抽象,然后封装呢?那么来看看javascr...
  • wangyongxia921
  • wangyongxia921
  • 2014年03月07日 21:13
  • 3052

JavaScript中原型对象的彻底理解

一、什么是原型 原型是Javascript中的继承的继承,JavaScript的继承就是基于原型的继承。 1.1 函数的原型对象​ 在JavaScript中,我们创建一个函数A(就是声明一个...
  • u012468376
  • u012468376
  • 2016年11月10日 20:43
  • 10672

JavaScript的各种对象

{===========1window对象============} window 代表了一个新开的窗口 window对象常用的方法:open() 打开一个新的窗口。setInterval() ...
  • slny1921394421
  • slny1921394421
  • 2016年07月20日 19:43
  • 603

JavaScript面向对象的三大特征

本来打算写设计模式的,在谈论js的设计模式之前先说一下js的面向对象,因为很多设计模式的实现都掺杂着面向对象的思想,所以先做一下前期铺垫。 js我们都知道是一种动态类型脚本型语言,变量类型无法保...
  • hbsxjs
  • hbsxjs
  • 2017年02月11日 22:26
  • 1829

window 窗口对象 - Javascript语言描述

转载地址:http://blog.csdn.net/ahpo/article/details/2379696 window 窗口对象 - Javascript语言描述 ---------...
  • stpeace
  • stpeace
  • 2016年02月28日 11:20
  • 1001

JavaScript中String对象常见方法及属性

JavaScript String对象简介在JavaScript,基本数据类型有Number型、String型、Boolean型、Undefined型、Null型、Function型等。String对...
  • a153375250
  • a153375250
  • 2016年03月30日 12:57
  • 1935

javascript中的面向对象理解(一)

一、注意:提到“面向对象”这一概念,众所周知,javascript中的面向对象思想与其他的编程语言(例如:PHP、Java等)是有着很大区别的。因此,我们先复习下,传统意义上,面向对象的相关概念,以便...
  • u014516981
  • u014516981
  • 2016年10月19日 23:48
  • 1883
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:JavaScript的对象
举报原因:
原因补充:

(最多只允许输入30个字)