当前页面: 开发资料首页 → Javascript 专题 → javascript手冊-s2
摘要: javascript
Sets the value of a date object.
dateObjectName.setTime(timevalue)
dateObjectName is either the name of a date object or a property of an existing object.
timevalue is an integer or a property of an existing object, representing the number of milliseconds since the epoch (1 January 1970 00:00:00).
Date
Use the setTime method to help assign a date and time to another date object.
theBigDay = new Date("July 1, 1999") sameAsBigDay = new Date() sameAsBigDay.setTime(theBigDay.getTime())
Evaluates an expression after a specified number of milliseconds have elapsed.
timeoutID=setTimeout(expression, msec)
timeoutID is an identifier that is used only to cancel the evaluation with the clearTimeout method.
expression is a string expression or a property of an existing object.
msec is a numeric value, numeric string, or a property of an existing object in millisecond units.
frame, window
The setTimeout method evaluates an expression after a specified amount of time. It does not evaluate the expression repeatedly. For example, if a setTimeout method specifies 5 seconds, the expression is evaluated after 5 seconds, not every 5 seconds.
Example 1. The following example displays an alert message 5 seconds (5,000 milliseconds) after the user clicks a button. If the user clicks the second button before the alert message is displayed, the timeout is canceled and the alert does not display.
<input TYPE="button" VALUE="5-second reminder" NAME="remind_button" onClick="timerID=setTimeout('displayAlert()',5000)"> <input TYPE="button" VALUE="Clear the 5-second reminder" NAME="remind_disable_button" onClick="clearTimeout(timerID)"> </form> </body>
Example 2. The following example displays the current time in a text object. The showtime() function, which is called recursively, uses the setTimeout method update the time every second.
Sets the year for a specified date.
dateObjectName.setYear(yearValue)
dateObjectName is either the name of a date object or a property of an existing object.
yearValue is an integer greater than 1900 or a property of an existing object.
Date
theBigDay.setYear(96)
Returns the sine of a number.
Math.sin(number)
number is a numeric expression or a property of an existing object, representing the size of an angle in radians.
Math
The sin method returns a numeric value between -1 and 1, which represents the sine of the angle.
//Displays the value 1 document.write("The sine of pi/2 radians is " + Math.sin(Math.PI/2)) //Displays the value 1.224606353822377e-016 document.write("The sine of pi radians is " + Math.sin(Math.PI)) //Displays the value 0 document.write("
The sine of 0 radians is " + Math.sin(0))
Causes a string to be displayed in a small font as if it were in a tag.
语法
stringName.small()
stringName is any string or a property of an existing object.
string
Use the small method with the write or writeln methods to format and display a string in a document.
var worldString="Hello, world" document.write(worldString.small()) document.write("" + worldString.big()) document.write("
" + worldString.fontsize(7))
The previous example produces the same output as the following htm:
Hello, worldHello, world
Hello, world
Returns the square root of a number.
Math.sqrt(number)number is any non-negative numeric expression or a property of an existing object.
Math
If the value of number is outside the suggested range, the return value is always 0.
//Displays the value 3 document.write("The square root of 9 is " + Math.sqrt(9)) //Displays the value 1.414213562373095 document.write("The square root of 2 is " + Math.sqrt(2)) //Displays the value 0 because the argument is out of range document.write("
The square root of -1 is " + Math.sqrt(-1))
The square root of one-half; equivalently, one over the square root of two, approximately 0.707.
Math.SQRT1_2
Math
Because SQRT1_2 is a constant, it is a read-only property of Math.
The following example displays 1 over the square root of 2:
document.write("1 over the square root of 2 is " + Math.SQRT1_2)
The square root of two, approximately 1.414.
Math.SQRT2
Math
Because SQRT2 is a constant, it is a read-only property of Math.
The following example displays the square root of 2:
document.write("The square root of 2 is " + Math.SQRT2)
Specifies a priority or transient message in the status bar at the bottom of the window, such as the message that appears when a mouseOver event occurs over an anchor.
windowReference.status
windowReference is a valid way of referring to a window, as described in the window object.
window
Do not confuse the status property with the defaultStatus property. The defaultStatus property reflects the default message displayed in the status bar.
You can set the status property at any time. You must return true if you want to set the status property in the onMouseOver event handler.
Suppose you have created a JavaScript function called pickRandomURL() that lets you select a URL at random. You can use the onClick event handler of an anchor to specify a value for the HREF attribute of the anchor dynamically, and the onMouseOver event handler to specify a custom message for the window in the status property:
In the above example, the status property of the window is assigned to the window's self property, as self.status. As this example shows, you must return true to set the status property in the onMouseOver event handler.
Causes a string to be displayed as struck out text as if it were in a tag.
语法
stringName.strike()
stringName is any string or a property of an existing object.
string
Use the strike method with the write or writeln methods to format and display a string in a document.
var worldString="Hello, world" document.write(worldString.blink()) document.write("" + worldString.bold()) document.write("
" + worldString.italics()) document.write("
" + worldString.strike())
The previous example produces the same output as the following htm:
Hello, world
Hello, world
Hello, world
A series of characters.
To use a string object:
1. stringName.propertyName 2. stringName.methodName(parameters)stringName is the name of a string variable.
The string object is a built-in JavaScript object.
A string can be represented as a literal enclosed by single or double quotes; for example, "Netscape" or 'Netscape'.
The following statement creates a string variable.
Causes a string to be displayed as a subscript as if it were in a tag.
stringName is any string or a property of an existing object.
string
Use the sub method with the write or writeln methods to format and display a string in a document.
This is what a " + subText.sub() + " looks like.")
The previous example produces the same output as the following htm: This is what a subscript looks like.
sup method
Submits a form.
formName is the name of any form or an element in the forms array.
form
The submit method submits the specified form. It performs the same action as a submit button.
Use the submit method to send data back to an http server. The submit method returns the data using either "get" or "post", as specified in the method property.
The following example submits a form called musicChoice: If musicChoice is the first form created, you also can submit it as follows: 相关 the example for the form object.
A submit button on an htm form. A submit button causes a form to be submitted.
To define a submit button, use standard htm 语法 with the addition of the onClick event handler: To use a submit object's properties and methods: A submit object on a form looks as follows:
<form>
<input type=submit value=Done> </form>
A submit object is a form element and must be defined within a <form> tag.
Clicking a submit button submits a form to the URL 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.
The submit button's onClick event handler cannot prevent a form from being submitted; instead, use the form's onSubmit event handler or use the submit method instead of a submit object. See the 例子 for the form object.
The following example creates a submit object called submit_button. The text "Done" is displayed on the face of the button. 相关 the 例子 for the form object.
Returns a subset of a string object.
stringName is any string or a property of an existing object. string
Characters in a string are indexed from left to right. The index of the first character is 0, and the index of the last character is stringName.length - 1.
If indexA is less than indexB, the substring method returns the subset starting with the character at indexA and ending with the character before indexB. If indexA is greater than indexB, the substring method returns the subset starting with the character at indexB and ending with the character before indexA. If indexA is equal to indexB, the substring method returns the empty string.
Causes a string to be displayed as a superscript as if it were in a tag.
stringName is any string or a property of an existing object.
string
Use the sup method with the write or writeln methods to format and display a string in a document.
This is what a " + subText.sub() + " looks like.")
The previous example produces the same output as the following htm: This is what a subscript looks like.
语法
stringName.sub()
方法
描述
例子
The following example uses the sub and sup methods to format a string: var superText="superscript"
var subText="subscript"
document.write("This is what a " + superText.sup() + " looks like.")
document.write("
This is what a superscript looks like.
相关
submit method
语法
formName.submit()
方法
描述
例子
document.musicChoice.submit()
document.forms[0].submit()
相关
submit object
语法
<input
TYPE="submit"
NAME=""
VALUE="buttonText"
[onClick="handlerText"]>
NAME="submitName" specifies the name of the submit object. You can access this value using the name property.
VALUE="buttonText" specifies the label to display on the button face. You can access this value using the value property.
1. submitName.propertyName
2. submitName.methodName(parameters)
3. formName.elements[index].propertyName
4. formName.elements[index].methodName(parameters)
submitName is the value of the NAME attribute of a submit object.
formName is either the value of the NAME attribute of a form object or an element in the forms array.
index is an integer representing a submit object on a form.
propertyName is one of the properties listed below.
methodName is one of the methods listed below.
Property of
描述
Properties
Methods
Event handlers
例子
相关
substring method
语法
stringName.substring(indexA, indexB)
indexA is any integer from 0 to stringName.length - 1, or a property of an existing object.
indexB is any integer from 0 to stringName.length - 1, or a property of an existing object.
方法
描述
例子
The following example uses substring to display characters from the string "Netscape". var anyString="Netscape"
//Displays "Net"
document.write(anyString.substring(0,3))
document.write(anyString.substring(3,0))
//Displays "cap"
document.write(anyString.substring(4,7))
document.write(anyString.substring(7,4))
sup method
语法
stringName.sup()
方法
描述
例子
The following example uses the sub and sup methods to format a string: var superText="superscript"
var subText="subscript"
document.write("This is what a " + superText.sup() + " looks like.")
document.write("
This is what a superscript looks like.
相关
document.write("Last modified " + document.lastModified)
Last modified 01/01/1998 23:55:24