javascript手冊-l
2007-11-27 17:51:58 来源:WEB开发网lastIndexOf method
Returns the index within the calling string object of the last occurrence of the specified value. The calling string is searched backwards, starting at fromIndex.
语法
stringName.lastIndexOf(searchValue, [fromIndex])
stringName is any string or a property of an existing object.
searchValue is a string or a property of an existing object, representing the value to search for.
fromIndex is the location within the calling string to start the search from. It can be any integer from 0 to stringName.length - 1 or a property of an existing object.
Method of
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 you do not specify a value for fromIndex, JavaScript assumes stringName.length - 1 (the end of the string) by default. If searchValue is not found, JavaScript returns -1.
例子
The following example uses indexOf and lastIndexOf to locate values in the string Brave new world.var anyString=Brave new world//Displays 8document.write(<P>The index of the first w from the beginning is + anyString.indexOf(w))//Displays 10document.write(<P>The index of the first w from the end is + anyString.lastIndexOf(w))//Displays 6document.write(<P>The index of new from the beginning is + anyString.indexOf(new))//Displays 6document.write(<P>The index of new from the end is + anyString.lastIndexOf(new))
相关
lastModified property
A string representing the date that a document was last modified.
语法
document.lastModified
Property of
document
描述
lastModified is a read-only property.
例子
In the following example, the lastModified property is used in a <SCRIPT> tag at the end of an htm file to display the modification date of the page:
document.write(This page updated on + document.lastModified)
length property
An integer that specifies a length-related feature of the calling object or array.
语法
When used with objects:
1. formName.length2. frameReference.length3. history.length4. radioName.length5. selectName.length6. stringName.length7. windowReference.length
When used with array properties:
8. anchors.length9. elements.length10. forms.length11. frameReference.frames.length12. windowReference.frames.length13. links.length14. selectName.options.length
formName is either the name of a form or an element in the forms array.
frameReference is either the value of the NAME attribute of a frame or an element in the frames array.
radioName is either the value of the NAME attribute of a radio object or an element in the elements array.
selectName is either the value of the NAME attribute of a select object or an element in the elements array.
stringName is any string or a property of an existing object.
windowReference is a valid way of referring to a window, as described in the window object.
Property of
描述
The length property is an integer that specifies one of the following:
length is always a read-only property.
For a null string, length is zero. For a select object, the values returned by form 5 and form 14 of the 语法 are the same. For a window containing frames, the values returned by form 7 and form 12 of the 语法 are the same. For a form object, the values returned by form 1 and form 9 of the 语法 are the same. For a frame containing frames, the values returned by form 2 and form 11 of the 语法 are the same.
例子
In the following example, the getChoice() function uses the length property to iterate over every element in the musicType array. musicType is a select element on the musicForm form.
function getChoice() { for (var i = 0; i < document.musicForm.musicType.length; i++) { if (document.musicForm.musicType.options[i].selected == true) { return document.musicForm.musicType.options[i].text } }}
The following example displays 8 in an alert dialog box:
var x=Netscapealert(The string length is + x.length)
link method
Creates an htm hypertext link that jumps to another URL.
语法
linkText.link(hrefAttribute)
linkText is any string or a property of an existing object.
hrefAttribute is any string or a property of an existing object.
Method of
string
描述
Use the link method with the write or writeln methods to programatically create and display a hypertext link in a document. Create the link with the link method, then call write or writeln to display the link in a document.
In the 语法, the linkText string represents the literal text that you want the user to see. The hrefAttribute string represents the HREF attribute of the <A> tag, and it should be a valid URL. Each section of a URL contains different information. See the location object for a 描述 of the URL components.
Links created with the link method become elements in the links array. See the link object for information about the links array.
例子
The following example displays the word Netscape as a hypertext link that returns the user to the Netscape home page:
var hotText=Netscapevar URL=http://www.netscape.comdocument.open()document.write(Click to return to + hotText.link(URL))document.close()
The previous example produces the same output as the following htm:
Click to return to <A HREF=http://www.netscape.com>Netscape</A>
相关
link object (links array)
A piece of text or an image identified as a hypertext link. When the user clicks the link text, the link hypertext reference is loaded into its target window.
语法
To define a link, use standard htm 语法 with the addition of the onClick and onMouseOver event handlers:
<A HREF=locationOrURL [NAME=anchorName] [TARGET=windowName] [onClick=handlerText] [onMouseOver=handlerText]> linkText</A>HREF=locationOrURL identifies a destination anchor or URL. See the location object for a 描述 of the URL components.
NAME=anchorName specifies a tag that becomes an available hypertext target within the current document. If this attribute is present, the link object is also an anchor object. See anchor for details.
TARGET=windowName specifies the window that the link is loaded into. windowName can be an existing window; it can be a frame name specified in a <FRAMESET> tag; or it can be one of the literal frame names _top, _parent, _self, or _blank; it cannot be a JavaScript expression (for example, it cannot be parent.frameName or windowName.frameName).
linkText is rendered as a hypertext link to the URL.
You can also define a link using the link method.
To use a link objects properties:
document.links[index].propertyNameindex is an integer representing a link object.
propertyName is one of the properties listed below.
Property of
描述
Each link object is a location object and has the same properties as a location object.
If a link object is also an anchor object, the object has entries in both the anchors and links arrays.
When a user clicks a link object and navigates to the destination document (specified by HREF=locationOrURL), the destination documents referrer property contains the URL of the source document. Evaluate the referrer property from the destination document.
The links array
You can reference the link objects in your code by using the links array. This array contains an entry for each link object (<A HREF=> tag) in a document in source order. For example, if a document contains three link objects, these links are reflected as document.links[0], document.links[1], and document.links[2].
To use the links array:
1. document.links[index]2. document.links.length
index is an integer representing a link in a document.
To obtain the number of links in a document, use the length property: document.links.length.
Elements in the links array are read-only. For example, the statement document.links[0]=link1 has no effect.
Properties
The link object has the following properties:
The links array has the following properties:
Methods
Event handlers
例子
Example 1. The following example creates a hypertext link to an anchor named javascript_intro.
Example 2. The following example creates a hypertext link to an anchor named numbers in the file DOC3.htm in the window window2. If window2 does not exist, it is created.
Example 3. The following example takes the user back x entries in the history list:
Example 4. The following example creates a hypertext link to a URL. A set of radio buttons lets the user choose between three URLs. The links onClick event handler sets the URL (the links href property) based on the selected radio button. The link also has an onMouseOver event handler that changes the windows status property. As the example shows, you must return true to set the window.status property in the onMouseOver event handler.
Example 5: links array. The following example opens the Netscape home page in the newWindow window. The linkGetter() function uses the links array to display the value of each of its links.
) }}
相关
linkColor property
A string specifying the color of the document hyperlinks.
语法
document.linkColor
Property of
document
描述
The linkColor 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 LINK attribute of the <BODY> tag. The default value of this property is set by the user on the Colors tab of the Preferences dialog box, which is displayed by choosing General Preferences from the Options menu. 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 document links to aqua using a string literal:
document.linkColor=aqua
The following example sets the color of document links to aqua using a hexadecimal triplet:
document.linkColor=00FFFF
相关
links property
An array of objects corresponding to link objects in source order. See link object.
LN2 property
The natural logarithm of two, approximately 0.693.
语法
Math.LN2
Property of
Math
描述
Because LN2 is a constant, it is a read-only property of Math.
例子
The following example displays the natural log of 2
更多精彩
赞助商链接