当前位置: 首页 > 图文教程 > 网络编程 > Javascript > javascript手冊-a
Returns the absolute value of a number.
Math.abs(number)
number is any numeric expression or a property of an existing object.
Math
In the following example, the user enters a number in the first text box and presses the Calculate button to display the absolute value of the number.
<FORM> <P>Enter a number: <INPUT TYPE="text" NAME="absEntry"> <P>The absolute value is: <INPUT TYPE="text" NAME="result"> <P> <INPUT TYPE="button" VALUE="Calculate" onClick="form.result.value = Math.abs(form.absEntry.value)"> </FORM>
Returns the arc cosine (in radians) of a number.
Math.acos(number)
number is a numeric expression between -1 and 1, or a property of an existing object.
Math
The acos method returns a numeric value between 0 and pi radians. If the value of number is outside the suggested range, the return value is always 0.
// Displays the value 0
document.write("The arc cosine of 1 is " + Math.acos(1))
// Displays the value 3.141592653589793
document.write("<P>The arc cosine of -1 is " + Math.acos(-1))
// Displays the value 0
document.write("<P>The arc cosine of 2 is " + Math.acos(2))
A string specifying a destination URL for form data that is submitted.
formName.action
formName is either the name of a form or an element in the forms array.
form
The action property is a reflection of the ACTION attribute of the <FORM> tag. Each section of a URL contains different information. See the location object for a 描述 of the URL components.
You can set the action property at any time.
Certain values of the action property may require specific values for other form properties. See RFC 1867 for more information.
The following example sets the action property of the musicForm form to the value of the variable urlName:
document.musicForm.action=urlName
Displays an Alert dialog box with a message and an OK button.
alert("message")
message is any string or a property of an existing object.
window
Use 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.
Although alert is a 用法 the window object, you do not need to specify a windowReference when you call it. For example, windowReference.alert() is unnecessary.
In the following example, the testValue() function checks the name entered by a user in the text object 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 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 object, as shown in the following example: Name: <INPUT TYPE="text" NAME="userName" onBlur="testValue(userName.value)">
A string specifying the color of an active link (after mouse-button down, but before mouse-button up).
document.alinkColor
document
The alinkColor property is expressed as a hexadecimal RGB triplet or as one of the string literals listed in Color Values. This property is the JavaScript reflection of the ALINK attribute of the <BODY> tag. You cannot set this property after the htm source has been through layout.
If you express the color as a hexadecimal RGB triplet, you must use the format rrggbb. For example, the hexadecimal RGB values for salmon are red=FA, green=80, and blue=72, so the RGB triplet for salmon is "FA8072".
The following example sets the color of active links to aqua using a string literal:
document.alinkColor="aqua"
The following example sets the color of active links to aqua using a hexadecimal triplet:
document.alinkColor="00FFFF"
Creates an htm anchor that is used as a hypertext target.
text.anchor(nameAttribute)
text is any string or a property of an existing object.
nameAttribute is any string or a property of an existing object.
string
Use the anchor method with the write or writeln methods to programatically create and display an anchor in a document. Create the anchor with the anchor method, then call write or writeln to display the anchor in a document.
In the 语法, the text string represents the literal text that you want the user to see. The nameAttribute string represents the NAME attribute of the <A> tag.
Anchors created with the anchor method become elements in the anchors array. See the anchor object for information about the anchors array.
The following example opens the msgWindow window and creates an anchor for the Table of Contents:
The previous example produces the same output as the following htm:
A piece of text that can be the target of a hypertext link.
To define an anchor, use standard htm 语法:
<A [HREF=locationOrURL] NAME="anchorName" [TARGET="windowName"]> anchorText </A>HREF=locationOrURL identifies a destination anchor or URL. If this attribute is present, the anchor object is also a link object. See link for details.
You can also define an anchor using the anchor method.
If an anchor object is also a link object, the object has entries in both the anchors and links arrays.
You can reference the anchor objects in your code by using the anchors array. This array contains an entry for each <A> tag containing a NAME attribute in a document in source order. For example, if a document contains three named anchors, these anchors are reflected as document.anchors[0], document.anchors[1], and document.anchors[2].
To use the anchors array:
1. document.anchors[index] 2. document.anchors.length
index is an integer representing an anchor in a document.
To obtain the number of anchors in a document, use the length property: document.anchors.length.
Even though the anchors array represents named anchors, the value of anchors[index] is always null. But if a document names anchors in a systematic way using natural numbers, you can use the anchors array and its length property to validate an anchor name before using it in operations such as setting location.hash. See the example below.
Elements in the anchors array are read-only. For example, the statement document.anchors[0]="anchor1" has no effect.
The anchors object has no properties. The anchors array has the following properties:
Example 1: an anchor. The following example defines an anchor for the text "Welcome to JavaScript".
If the preceding anchor is in a file called intro.htm, a link in another file could define a jump to the anchor as follows:
Example 2: anchors array. The following example opens two windows. The first window contains a series of buttons that set location.hash in the second window to a specific anchor. The second window defines four anchors named "0", "1", "2", and "3". (The anchor names in the document are therefore 0, 1, 2, ... (document.anchors.length-1)). When a button is pressed in the first window, the onClick event handler verifies that the anchor exists before setting window2.location.hash to the specified anchor name.
LINK1.htm, which defines the first window and its buttons, contains the following code:
LINK2.htm, which contains the anchors, contains the following code:
Some colors (Anchor 1)
Some music types (Anchor 2)
Some countries (Anchor 3)
An array of objects corresponding to named anchors in source order. See anchor object.
A string specifying the code name of the browser.
navigator.appCodeName
navigator
appCodeName is a read-only property.
The following example displays the value of the appCodeName property:
document.write("The value of navigator.appCodeName is " + navigator.appCodeName)
For Navigator 2.0, this displays the following:
The value of navigator.appCodeName is Mozilla
A string specifying the name of the browser.
navigator.appName
navigator
appName is a read-only property.
The following example displays the value of the appName property:
document.write("The value of navigator.appName is " + navigator.appName)
For Navigator 2.0, this displays the following:
The value of navigator.appName is Netscape
A string specifying version information for the Navigator.
navigator.appVersion
navigator
The appVersion property specifies version information in the following format:
releaseNumber (platform; country)
The values contained in this format are the following:
appVersion is a read-only property.
Example 1. The following example displays version information for the Navigator:
document.write("The value of navigator.appVersion is " + navigator.appVersion)
For Navigator 2.0 on Windows 95, this displays the following:
The value of navigator.appVersion is 2.0 (Win95, I)
Example 2. The following example populates a textarea object with newline characters separating each line. Because the newline character varies from platform to platform, the example tests the appVersion property to determine whether the user is running Windows (appVersion contains "Win" for all versions of Windows). If the user is running Windows, the newline character is set to \r\n; otherwise, it's set to \n, which is the newline character for Unix and Macintosh.
Returns the arc sine (in radians) of a number.
Math.asin(number)
number is a numeric expression between -1 and 1, or a property of an existing object.
Math
The asin method returns a numeric value between -pi/2 and pi/2 radians. If the value of number is outside the suggested range, the return value is always 0.
// Displays the value 1.570796326794897 (pi/2)
document.write("The arc sine of 1 is " + Math.asin(1))
// Displays the value -1.570796326794897 (-pi/2)
document.write("<P>The arc sine of -1 is " + Math.asin(-1))
// Displays the value 0 because the argument is out of range
document.write("<P>The arc sine of 2 is " + Math.asin(2))
Returns the arc tangent (in radians) of a number.
Math.atan(number)
number is either a numeric expression or a property of an existing object, representing the tangent of an angle.
Math
The atan method returns a numeric value between -pi/2 and pi/2 radians.
// Displays the value 0.7853981633974483
document.write("The arc tangent of 1 is " + Math.atan(1))
// Displays the value -0.7853981633974483
document.write("<P>The arc tangent of -1 is " + Math.atan(-1))
// Displays the value 0.4636476090008061
document.write("<P>The arc tangent of .5 is " + Math.atan(.5))
评论 (0) All