|
JavaScript的方法
以下的一些"方法"可用在JavaScript: abs methodReturns the absolute value of its argument. Syntaxabs(arg) Applies toExamplesxxx Examples to be supplied. acos methodReturns the arc cosine (in radians) of its argument. Syntaxacos(arg) Applies toExamplesxxx Examples to be supplied. See alsoalert methodDisplays an Alert dialog box with a message and an OK button. Syntaxalert("message") The argument message is any string. DescriptionUse the alert method to display a message that does not require a user decision. The message argument specifies a message that the dialog box contains. Applies toExamplesIn the following example, the testValue function checks the name entered by a user in the text element of a form to make sure that it is no more than eight characters in length. This example uses the alert method to prompt the user of an application to enter a valid value. function testValue(textElement) { if (textElement.length > 8) { alert("Please enter a name that is 8 characters or less") } }You can call the testValue function in the onBlur event handler of a form's text element, as shown in the following example: Name: <INPUT TYPE="text" NAME="userName" onBlur="testValue(userName)"> See alsoanchor methodCauses the calling string object to be a named anchor by surrounding it with HTML anchor tags, <A NAME=...> </A>. Syntaxstring.anchor(name) The argument name can be any string. Descriptionxxx Description to be supplied. Applies toExamplesxxx Examples to be supplied. See alsoasin methodReturns the arc sine (in radians) of its argument. Syntaxasin(arg) Applies toExamplesxxx Examples to be supplied. See alsoassign methodSets location.href so you don't have to type ".href" complements toString(). Syntaxassign() DescriptionDo not call the assign method. It exists so you can use 'location=...' instead of 'location.href=...'. xxx Description to be supplied. Applies toExamplesxxx Examples to be supplied. atan methodReturns the arc tangent (in radians) of its argument. Syntaxatan(arg) Applies toExamplesxxx Examples to be supplied. See alsoback methodLoads the previous URL in the history list. Syntaxback() DescriptionThis method performs the same action as a user choosing the Back button in the Navigator. The back method is the same as go(-1). Applies toExamplesxxx Examples to be supplied. See alsobig methodCauses the calling string object to be displayed in big font by surrounding it with HTML big font tags, <BIG> and </BIG>. Syntaxbig() Descriptionxxx Description to be supplied. Applies toExamplesxxx Examples to be supplied. See alsoblink methodCauses the calling string object to blink by surrounding it with HTML blink tags, <BLINK> ... </BLINK>. Syntaxblink() Descriptionxxx Description to be supplied. Applies toExamplesxxx Examples to be supplied. See alsoblur methodFor password, text, and textArea, removes focus to the object. Syntaxblur() DescriptionUse the blur method to remove focus from a specific form element. Applies toExamplesxxx Examples to be supplied. See alsobold methodCauses the calling string object to be displayed as bold by surrounding it with HTML bold tags, <B> ... </B>. Syntaxbold()NOTE: For Beta4, use B() for this method. Descriptionxxx Description to be supplied. Applies toExamplesxxx Examples to be supplied. See alsoceil methodReturns the least integer greater than or equal to its argument. Syntaxceil(arg) Applies toExamplesxxx Examples to be supplied. See alsocharAt methodReturns the character at the specified index. SyntaxcharAt(index) index is any integer from 0 to string.length() - 1. Descriptionxxx Description to be supplied. Applies toExamplesxxx Examples to be supplied. See alsoclear methodClears the window. Syntaxclear() Descriptionxxx Description to be supplied. Applies toExamplesxxx Examples to be supplied. clearTimeout methodCancels a timeout that was set with the setTimeout method. SyntaxclearTimeout(timeoutID) timeoutID is a timeout setting that was returned by a previous call to the setTimeout method. Descriptionxxx Description to be supplied. Applies to Examplesxxx Examples to be supplied. See alsoclick methodSimulates a mouse click on the calling form element. Syntaxclick() DescriptionThe effect of the click method varies according to the calling element: Applies tobutton, checkbox, radioButton, reset, selection, submit Examplesxxx Examples to be supplied. close methodFor document and window, closes the window. xxx Not yet implemented in Beta 4. Syntaxclose() DescriptionDocument description to be supplied. The close method closes the current window. Applies toExamplesAny of the following examples close the current window: window.close() self.close() close() See alsoconfirm methodDisplays a Confirm dialog box with the specified message and OK and Cancel buttons. Syntaxconfirm("message") The argument message is any string. DescriptionUse the confirm method to ask the user to make a decision that requires either an OK or a Cancel. The message argument specifies a message that prompts the user for the decison. The confirm method returns true if the user chooses OK and false if the user chooses Cancel. Applies toExamplesThis example uses the confirm method in the confirmCleanUp function to confirm that the user of an application really wants to quit. If the user chooses OK, the custom cleanUp() function closes the application. function confirmCleanUp() { if (confirm("Are you sure you want to quit this application?")) { cleanUp() } }You can call the confirmCleanUp function in the onClick event handler of a form's pushbutton, as shown in the following example: <INPUT TYPE="button" VALUE="Quit" onClick="confirmCleanUp()"> See alsocos methodReturns the cosine of its argument. The argument is in radians. Syntaxcos(arg) Applies toExamplesxxx Examples to be supplied. See alsoeval functionThe eval function is a function built-in to JavaScript. It is not a method associated with any object, but is part of the language itself. Syntaxeval(expression) DescriptionThe eval function takes a JavaScript arthimetic expression as its argument and returns the value of the argument as a number. ExampleBoth of the uses of eval in the following example assign the value 42 to the variable result. x = 6 result = eval((3+3)*7) result = eval(x*7) exp methodReturns e to the power of its argument, i.e. ex, where x is the argument, and e is Euler's constant, the base of the natural logarithms. Syntaxexp(arg) Applies toExamplesxxx Examples to be supplied. See alsofixed methodCauses the calling string object to be displayed in fixed-pitch font in HTML by surrounding it with typewriter text tags, <fixed> ... </fixed>. Syntaxfixed()NOTE: For Beta4, use TT() for this method. Descriptionxxx Description to be supplied. Applies toExamplesxxx Examples to be supplied. floor methodReturns the greatest integer less than or equal to its argument. Syntaxfloor(arg) Applies toExamplesxxx Examples to be supplied. See alsofocus methodFor password, text, and textArea, gives focus to the object. Syntaxfocus() DescriptionUse the focus method to navigate to a specific form element and give it focus. You can then either programatically enter a value in the element or let the user enter a value. Applies toExamplesIn the following example, the checkPassword function confirms that a user has entered a valid password. If the password is not valid, the focus method returns focus to the password field and the select method highlights it so the user can re-enter the password. function checkPassword(userPass) { if (badPassword) { alert("Please enter your password again.") userPass.focus() userPass.select() } }This example assumes that the password is defined as: <INPUT TYPE=password NAME=userPass> See alsofontcolor methodCauses the calling string object to be displayed in the specified color by surrounding it with HTML font color tags, <FONTCOLOR=color> ... </FONTCOLOR>. Syntaxfontcolor(color) The argument to the method, color, must be a string containing a hashmark (#) followed by a triplet of hexadecimal number pairs. These three pairs represent the red, green, and blue values for the desired color, respectively. Descriptionxxx Description to be supplied. Applies toExamplesxxx Examples to be supplied. fontsize methodCauses the calling string object to be displayed in the specified font size by surrounding it with HTML font size tags, <FONTSIZE=size> ... </FONTSIZE>. Syntaxfontsize(size) The argument to the method, size, must be an integer between one and seven. Descriptionxxx Description to be supplied. Applies toExamplesxxx Examples to be supplied. See alsoforward methodLoads the next URL in the history list. Syntaxforward() DescriptionThis method performs the same action as a user choosing the Forward button in the Navigator. The forward method is the same as go(1). Applies toExamplesxxx Examples to be supplied. See alsogetDate methodSyntaxdateObj.getDate()
where DescriptionReturns the day of the month for the date object, an integer between 1 and 31. Applies toExamplesThe second statement below assigns the value 25 to the variable day, based on the value of the date object Xmas95. Xmas95 = new Date("December 25, 1995 23:15:00") day = Xmas95.getDate() See alsogetDay methodSyntaxdateObj.getDay()
where DescriptionReturns the day of the week for the date object, an integer corresponding to the day of the week: zero for Sunday, one for Monday, two for Tuesday, and so on. Applies toExamplesThe second statement below assigns the value 1 to weekday, based on the value of the date object Xmas95. This is because December 25, 1995 is a Monday. Xmas95 = new Date("December 25, 1995 23:15:00") weekday = Xmas95.getDay() See alsogetHours methodSyntaxdateObj.getHours()
where DescriptionReturns the hour for the date object, an integer between 0 and 23. Applies toExamplesThe second statement below assigns the value 23 to the variable hours, based on the value of the date object Xmas95. Xmas95 = new Date("December 25, 1995 23:15:00") hours = Xmas95.getHours() See alsogetMinutes methodSyntaxdateObjgetMinute()
where DescriptionReturns the minutes in the date object, an integer between 0 and 59. Applies toExamplesThe second statement below assigns the value 15 to the variable minutes, based on the value of the date object Xmas95. Xmas95 = new Date("December 25, 1995 23:15:00") minutes = Xmas95.getMinutes() See alsogetMonth methodSyntaxdateObj.getMonth()
where DescriptionReturns the month in the date object, an integer between zero and eleven. Zero corresponds to January, one to Februrary, and so on. Applies toExamplesThe second statement below assigns the value 11 to the variable month, based on the value of the date object Xmas95. Xmas95 = new Date("December 25, 1995 23:15:00") month = Xmas95.getDate() See alsogetSeconds methodSyntax
where DescriptionReturns the seconds in the current time, an integer between 0 and 59. Applies toExamplesThe second statement below assigns the value 30 to the variable secs, based on the value of the date object Xmas95. Xmas95 = new Date("December 25, 1995 23:15:30") secs = Xmas95.getSeconds() See alsogetTime methodSyntaxdateObj.getTime()
where DescriptionReturns the numeric value for the date object. This number is the number of milliseconds since the epoch (1 January 1970 00:00:00) You can use this method to help assign a date and time to another date object. Applies toExamplesThe following example assigns the date value of theBigDay to sameAsBigDay. theBigDay = new Date("July 1, 1999") sameAsBigDay = new Date(); sameAsBigDay.setTime(theBigDay.getTime()) See alsogetTimezoneOffset methodSyntaxdateObj.getTimezoneOffset()
where DescriptionReturn the time zone offset in minutes for the current locale, i.e. the difference between the local time and GMT. This value would be a constant except for daylight savings time. Applies toExamplesx = new Date(); currentTimeZoneOffsetInHours = x.getTimezoneOffset()/60; See alsogetYear methodSyntaxgetyear() DescriptionReturns the year in the date object, less 1900. Applies toExamplesThe second statement below assigns the value 95 to the variable year, based on the value of the date object Xmas95. Xmas95 = new Date("December 25, 1995 23:15:00") year = Xmas95.getYear() See alsogo methodThe argument delta is an integer. If delta is greater than zero, then it loads the URL that is that number of entries forward in the history list; otherwise, it loads the URL that is that number of entries backward in the history list. The argument string is a string. Go to the newest history entry whose title or URL contains string as a substring; substring matching is case-insensitive. Syntaxgo(delta | "string") Descriptionxxx Description to be supplied. Applies toExamplesxxx Examples to be supplied. See alsoindexOf methodReturns the index within the calling string object of the first occurrence of the specified character, starting the search at fromIndex. SyntaxindexOf(character, [fromIndex]) character is the character to search for.
Descriptionxxx Description to be supplied. Applies toExamplesxxx Examples to be supplied. See alsoitalics methodCauses the calling string object to be italicized by surrounding it with HTML italics tags, <I> ... </I>. Syntaxitalics()NOTE: For Beta4, use I() for this method. Descriptionxxx Description to be supplied. Applies toExamplesxxx Examples to be supplied. See alsolastIndexOf methodReturns the index within the calling string object of the last occurrence of the specified character. The calling string is searched backwards, starting at fromIndex. SyntaxindexOf(character, [fromIndex]) character is the character to search for.
Descriptionxxx Description to be supplied. Applies toExamplesxxx Examples to be supplied. See alsolink methodCauses the calling string object to be a hyperlink by surrounding it with HTML hyperlink tags, <A HREF=...> </A>. Syntaxlink(location)The argument to the method, location, should be a valid URL. Descriptionxxx Description to be supplied. Applies toExamplesxxx Examples to be supplied. See alsolog methodReturns the natural logarithm (base e) of its argument. Syntaxlog(arg) Applies toExamplesxxx Examples to be supplied. See alsomax methodReturns the greater of its two arguments. Syntaxmax(arg1, arg2) Applies toExamplesxxx Examples to be supplied. See alsomin methodReturns the lesser of its two arguments. Syntaxmin(arg1, arg2) Applies toExamplesxxx Examples to be supplied. See alsoopen methodFor the window object, opens a new web browser window. xxx For the document object, description to be supplied. SyntaxThe first form of the syntax is for the document object; the second form is for window. 1. document.open("MIME type") 2. window.open("URL", "windowName", ["windowFeatures"])
URL specifies the URL to open in the new window.
toolbar[=yes|no]|[=1|0] location[=yes|no]|[=1|0] directories[=yes|no]|[=1|0] status[=yes|no]|[=1|0] menubar[=yes|no]|[=1|0] scrollbars[=yes|no]|[=1|0] resizable[=yes|no]|[=1|0] copyhistory[=yes|no]|[=1|0] width=pixels height=pixels You may use any subset of these options. Separate options with a comma. Do not put spaces between the options. pixels is a positive integer specifying the dimension in pixels. NOTE: In event handlers, you must specify window.open() unless you want to call document.open() because of the scoping of static objects in JavaScript. Description for windowThe open method opens a new web browser window on the client, similar to choosing File|New Web Browser from the menu of the Navigator. The URL argument specifies the URL contained by the new window. If URL is an empty string, a new, empty window is created. windowFeatures is an optional, comma-separated list of options for the new window. The boolean windowFeatures options are set to true if they are specified without values, or as yes or 1. For example, open("", "messageWindow", "toolbar") and open("", "messageWindow", "toolbar=1") both set the toolbar option to true. If windowName does not specify an existing window and you do not specify windowFeatures, all boolean windowFeatures are true by default. Following is a description of the windowFeatures: Description for documentDescription to be supplied. Applies toExamplesIn the following example, the windowOpener function opens a window and uses write methods to display a message: function windowOpener() { msgWindow=open("","Display window","toolbar=no,directories=no,menubar=no"); msgWindow.document.write("<HEAD><TITLE>Message window</TITLE>"); msgWindow.document.write("<CENTER><BIG><B>Hello, world!</B>"); } The following is an onClick event handler that opens a new client window displaying the content specified in the file sesame.html. It opens it with the specified option settings and names the corresponding window object newWin. Notice the use of single quotes (') inside the onClick event handler. See alsoparse methodSyntaxDate.parse(date string) DescriptionThe parse function takes a date string (such as "Dec 25, 1995"), and returns the number of milliseconds since January 1, 1970 00:00:00 (local time). This function is useful for setting date values based on string values, for example in conjunction with the setTime method. Given a string representing a time, parse returns the time value. It accepts the IETF standard date syntax: "Mon, 25 Dec 1995 13:30:00 GMT". It understands the continental US time zone abbreviations, but for general use, use a time zone offset, for example "Mon, 25 Dec 1995 13:30:00 GMT+0430" (4 hours, 30 minutes west of the Greenwich meridian). If you do not specify a time zone, the local time zone is assumed. GMT and UTC are considered equivalent.
The parse function is a static method of Date. That means that you always use it as
Applies toExamplesIf IPOdate is an existing date object, then IPOdate.setTime(Date.parse("Aug 9, 1995")) See alsopow methodReturns arg1 to the arg2 power, i.e. arg1arg2. Syntaxpow(arg1, arg2) Applies toExamplesxxx Examples to be supplied. See alsoprompt methodDisplays an Prompt dialog box with a message and an input field Syntaxprompt(message, input default) DescriptionUse the prompt method to display a dialog box that takes user input. The arguments, message and input default are JavaScript values to be displayed as the message and the default value in the input field, respectively. Applies toExamplesprompt("Enter the number of doughnuts you want:", 12) See alsorandom methodReturns a pseudo-random number between zero and one. xxx NYI. Syntaxxxx To be supplied. Applies toExamplesxxx Examples to be supplied. round methodReturns its argument, rounded to the nearest integer. In other words, if its argument is n.5 or greater (where n is an integer), returns n+1, otherwise returns n. Syntaxround(arg) Applies toExamplesxxx Examples to be supplied. select methodFor password, text, and textArea, selects the input area of the object. Syntaxselect() DescriptionUse the select method to highlight the input area of a form element. You can use the select method with the focus method to highlight a field and position the cursor for a user response. Applies toExamplesIn the following example, the checkPassword function confirms that a user has entered a valid password. If the password is not valid, the select method highlights the password field and focus method returns focus to it so the user can re-enter the password. function checkPassword(userPass) { if (badPassword) { alert("Please enter your password again.") userPass.focus() userPass.select() } }This example assumes that the password is defined as: <INPUT TYPE=password NAME=userPass> See alsosetDate methodSyntaxdateObj.setDate(day)
where DescriptionSets the day of the month for the date object. The argument is an integer from 1 to 31. Applies toExamplesThe second statement below changes the day for theBigDay to the 24th of July from its original value. theBigDay = new Date("July 27, 1962 23:30:00") Xmas95.setDate(24) See alsosetHours methodSyntaxdateObj.setHours(hours
where DescriptionSets the hours in the current time. The argument is an integer between 0 and 23. Applies toExamplestheBigDay.setHours(7) See alsosetMinutes methodSyntax
where DescriptionSets the minutes in the current time. The argument is an integer between 0 and 59. Applies toExamplestheBigDay.setMinutes(45) See alsosetMonth methodSyntaxdateObj.setMonth(month)
where DescriptionSets the month in the current date. The argument is an integer between 0 and 11. Applies toExamplestheBigDay.setMonth(6) See alsosetSeconds methodSyntaxdateObj.setSeconds(seconds)
where DescriptionSets the seconds in the current time. The argument is an integer between 0 and 59. Applies toExamplestheBigDay.setSeconds(30) See alsosetTime methodSyntax
where DescriptionSets the value of the date object. This argument is the number of milliseconds since the epoch (1 January 1970 00:00:00) You can use this method to help assign a date and time to another date object. Applies toExamplestheBigDay = new Date("July 1, 1999") sameAsBigDay = new Date(); sameAsBigDay.setTime(theBigDay.getTime()) setTimeout methodEvaluates an expression after a specified number of milliseconds have elapsed. SyntaxtimeoutID=setTimeout(expression, msec) expression is a string expression.
Descriptionxxx Description to be supplied. Applies to Examplesxxx Examples to be supplied. See alsosetYear methodSyntaxdateObj.setYear(year)
where DescriptionSets the year in the current date. Takes as its argument an integer greater than 1900. Applies toExamplestheBigDay.setYear(96) See alsosin methodReturns the sine of its argument. The argument is in radians. Syntaxsin(arg) Applies toExamplesxxx Examples to be supplied. See alsosmall methodCauses the calling string object to be displayed in small font by surrounding it with HTML small font tags, <SMALL>.... Syntaxsmall() Descriptionxxx Description to be supplied. Applies toExamplesxxx Examples to be supplied. See alsosqrt methodReturns the square root of its argument. Syntaxsqrt(arg) Applies toExamplesxxx Examples to be supplied. See alsostrike methodCauses the calling string object to be displayed as struck out text, by surrounding it with HTML strikeout tags, <STRIKE> ... </STRIKE>. Syntaxstrike() Descriptionxxx Description to be supplied. Applies toExamplesxxx Examples to be supplied. See alsosub methodCauses the calling string object to be displayed as a subscript by surrounding it with HTML subscript tags, <SUB> ... </SUB>. Syntaxsub() Descriptionxxx Description to be supplied. Applies toExamplesxxx Examples to be supplied. sup method submit methodSubmits the form. Syntaxsubmit() Descriptionxxx Description to be supplied. Applies toExamplesxxx Examples to be supplied. substring methodThe substring method returns a subset of a string object. If a < b, then it returns the subset starting at character a and ending before character b, considering the first character of the string to be character zero (0). If a > b, then it returns the subset starting at character b and ending at character a. If a = b, then it returns the empty string. Syntaxsubstring(a, b) Descriptionxxx Description to be supplied. Applies toExamplesIf string x is "netscape" then x.substring(0,3) or x.substring(3,0) returns "net", and x.substring(4,7) or x.substring(7,4) returns "cap". sup methodCauses the calling string object to be displayed as a superscript by surrounding it with HTML superscript tags, <SUP> ... </SUP>. Syntaxsup() Descriptionxxx Description to be supplied. Applies toExamplesxxx Examples to be supplied. See alsotan methodReturns the tangent of its argument. The argument is in radians. Syntaxtan(arg) Applies toExamplesxxx Examples to be supplied. See alsotoGMTString methodSyntaxdateObj.toGMTString()
where DescriptionConverts a date to a String, using the Internet GMT conventions. Applies toExamplestoday.toGMTString()
If today is a date object, then toGMTString would convert the date to
GMT (UTC) using the operating system's time zone offset, and return a string
value in the following form:
toLocaleString methodSyntaxdateObj.toLocaleString()
where DescriptionConverts a date to a String, using the locale conventions. Applies toExamplestoday.toLocaleString()
If today is a date object, then toLocaleString will return a string value
in the following form:
toLowerCase methodThe toLowerCase method of string objects converts the string to all upper case. SyntaxtoLowerCase() Descriptionxxx Description to be supplied. Applies toExamplesFor example, "ALPHABET".toLowerCase yields "alphabet". See alsotoString methodFor location, returns location.href, so you can use location as a string without typing ".href". For Date, converts a date to a string. SyntaxtoString() Descriptionxxx Description to be supplied. Applies toExamplesHere is an example of using toString with a date object, assuming theBigDay is a date object: theBigDay.toString() More Examples to be supplied. toUpperCase methodThe toUpperCase method of string objects converts the string to all upper case. SyntaxtoUpperCase() Descriptionxxx Description to be supplied. Applies toExamplesFor example, "alphabet".toUpperCase yields "ALPHABET". See alsoUTC methodSyntaxDate.UTC(year, month, mday [, hrs] [, min] [, sec])
DescriptionUTC takes comma-delimited date parameters and returns the number of milliseconds since January 1, 1970 00:00:00, Universal Coordinated Time (GMT). The arameters are:
UTC is a static method of Date. That means that you always use it as
Applies toExamplesThe following statement creates a date object using GMT instead of local time: gmtDate = new Date(Date.UTC(96, 11, 1, 0, 0, 0)) See alsowrite methodWrite HTML to the current window, in sequence with the HTML containing this SCRIPT. SCRIPTs have no effect on paragraphs or other structures in which they may occur. Syntaxwrite() Descriptionxxx Description to be supplied. Applies toExamplesxxx Examples to be supplied. See alsowriteln methodThe same as write(), but adds a newline character. Note that this affects only preformatted text (inside a <PRE> or <XMP> tag). Syntaxwriteln() Descriptionxxx Description to be supplied. Applies toExamplesxxx Examples to be supplied. See also回目录 |