分享
 
 
 

javascript手冊-l

王朝html/css/js·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

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 8

document.write("<P>The index of the first w from the beginning is " +

anyString.indexOf("w"))

//Displays 10

document.write("<P>The index of the first w from the end is " +

anyString.lastIndexOf("w"))

//Displays 6

document.write("<P>The index of 'new' from the beginning is " +

anyString.indexOf("new"))

//Displays 6

document.write("<P>The index of 'new' from the end is " +

anyString.lastIndexOf("new"))

相关

charAt, indexOf methods

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.length

2. frameReference.length

3. history.length

4. radioName.length

5. selectName.length

6. stringName.length

7. windowReference.length

When used with array properties: 8. anchors.length

9. elements.length

10. forms.length

11. frameReference.frames.length

12. windowReference.frames.length

13. links.length

14. 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

frame, history, radio, select, string, window objects

anchors, elements, forms, frames, links, options arrays

描述

The length property is an integer that specifies one of the following:

The number of elements on a form (form 1 of the 语法).

The number of frames within a frame (form 2 of the 语法). A frame that does not load a document containing a <FRAMESET> tag always has a length of 0.

The number of entries in a history object (form 3 of the 语法).

The number of radio buttons in a radio object (form 4 of the 语法).

The number of options in a select object (form 5 of the 语法).

The length of a string object (form 6 of the 语法).

The number of frames in a parent window (form 7 of the 语法).

The number of entries in one of the array properties (all other 语法 forms).

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="Netscape"

alert("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="Netscape"

var URL="http://www.netscape.com"

document.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>

相关

anchor method

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 object's properties: document.links[index].propertyName

index is an integer representing a link object.

propertyName is one of the properties listed below.

Property of

document

描述

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 document's 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:

hash specifies an anchor name in the URL

host specifies the hostname:port portion of the URL

hostname specifies the host and domain name, or IP address, of a network host

href specifies the entire URL

pathname specifies the url-path portion of the URL

port specifies the communications port that the server uses for communications

protocol specifies the beginning of the URL, including the colon

search specifies a query

target reflects the TARGET attribute

The links array has the following properties:

length reflects the number of links in a document

Methods

None.

Event handlers

onClick

onMouseOver

例子

Example 1. The following example creates a hypertext link to an anchor named javascript_intro. Introduction to JavaScript

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. Numbers

Example 3. The following example takes the user back x entries in the history list: Click here

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 link's onClick event handler sets the URL (the link's href property) based on the selected radio button. The link also has an onMouseOver event handler that changes the window's status property. As the example shows, you must return true to set the window.status property in the onMouseOver event handler.

Choose a destination from the following list, then click "Click me" below.

Netscape home page

Sun home page

RFC 1867

Click me

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. newWindow=window.open("http://www.netscape.com")

function linkGetter() {

msgWindow=window.open("")

for (var i = 0; i

")

}

}

相关

anchor object

link method

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"

相关

alinkColor, bgColor, fgColor, and vlinkColor properties

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: document.write("The natural log of 2 is " + Math.LN2)

相关

E, LN10, LOG2E, LOG10E, PI, SQRT1_2, SQRT2 properties

LN10 property

The natural logarithm of ten, approximately 2.302.

语法Math.LN10

Property of

Math

描述

Because LN10 is a constant, it is a read-only property of Math.

例子

The following example displays the natural log of 10: document.write("The natural log of 10 is " + Math.LN10)

相关

E, LN2, LOG2E, LOG10E, PI, SQRT1_2, SQRT2 properties

location object

Contains information on the current URL.

语法

To use a location object: [windowReference.]location[.propertyName]

windowReference is a variable windowVar from a window definition (see window object), or one of the synonyms top or parent.

propertyName is one of the properties listed below. Omitting the property name is equivalent to specifying the href property (the complete URL).

Property of

window

描述

The location object represents a complete URL. Each property of the location object represents a different portion of the URL.

The following diagram of a URL shows the relationships between the location properties: protocol//hostname:port pathname search hash

protocol represents the beginning of the URL, up to and including the first colon.

hostname represents the host and domain name, or IP address, of a network host.

port represents the communications port that the server uses for communications.

pathname represents the url-path portion of the URL.

search represents any query information in the URL, beginning with a question mark.

hash represents an anchor name fragment in the URL, beginning with a hash mark (#).

See the properties (listed below) for details about the different parts of the URL, or the href property for 例子.

The location object has two other properties not shown in the diagram above:

href represents a complete URL.

host represents the concatenation hostname:port.

The location object is contained by the window object and is within its scope. If you reference a location object without specifying a window, the location object represents the current location. If you reference a location object and specify a window name, for example, windowReference.location.propertyName, the location object represents the location of the specified window.

Do not confuse the location object with the location property of the document object. You cannot change the value of the location property (document.location), but you can change the value of the location object's properties (window.location.propertyName). document.location is a string-valued property that usually matches what window.location.href is set to when you load the document, but redirection may change it.

语法 for common URL types

When you specify a URL, you can use standard URL formats and JavaScript statements. The following list shows the 语法 for specifying some of the most common types of URLs.

URL type

Protocol

Example

JavaScript code

javascript:

javascript:history.go(-1)

Navigator info

about:

about:cache

World Wide Web

http:

http://www.netscape.com/

File

file:

file:///javascript/methods.htm

FTP

ftp:

ftp://ftp.mine.com/home/mine

MailTo

mailto:

mailto:info@netscape.com

Usenet

news:

news://news.scruznet.com/comp.lang.javascript

Gopher

gopher:

gopher.myhost.com

The javascript: protocol evaluates the expression after the colon (:), if there is one, and loads a page containing the string value of the expression, unless it is undefined. If the expression evaluates to undefined, no new page loads.

The about: protocol provides information on Navigator and has the following 语法: about:[cache|plugins]

about: by itself is the same as choosing About Netscape from the Navigator's Help menu.

about:cache displays disk cache statistics.

about:plug-ins displays information about plug-ins you have configured. This is the same as choosing About Plug-ins from the Navigator's Help menu.

Properties

hash specifies an anchor name in the URL

host specifies the hostname:port portion of the URL

hostname specifies the host and domain name, or IP address, of a network host

href specifies the entire URL

pathname specifies the url-path portion of the URL

port specifies the communications port that the server uses for communications

protocol specifies the beginning of the URL, including the colon

search specifies a query

Methods

None.

Event handlers

None.

例子

Example 1. The following two statements are equivalent and set the URL of the current window to the Netscape home page: window.location.href="http://www.netscape.com/"

window.location="http://www.netscape.com/"

Example 2. The following statement sets the URL of a frame named frame2 to the Sun home page: parent.frame2.location.href="http://www.sun.com/"

相关 the example for the anchor object.

相关

history object

location property

location property

A string specifying the complete URL of the document.

语法document.location

Property of

document

描述

Do not confuse the location property of the document object with the location object. You cannot change the value of the location property (document.location), but you can change the value of the location object's properties (window.location.propertyName). document.location is a string-valued property that usually matches what window.location.href is set to when you load the document, but redirection may change it.

location is a read-only property of document.

例子

The following example displays the URL of the current document: document.write("The current URL is " + document.location)

相关

location object

log method

Returns the natural logarithm (base e) of a number.

语法Math.log(number)

number is any positive numeric expression or a property of an existing object.

Method of

Math

描述

If the value of number is outside the suggested range, the return value is always -1.797693134862316e+308.

例子//Displays the value 2.302585092994046

document.write("The natural log of 10 is " + Math.log(10))

//Displays the value 0

document.write("<P>The natural log of 1 is " + Math.log(1))

//Displays the value -1.797693134862316e+308

//because the argument is out of range

document.write("<P>The natural log of 0 is " + Math.log(0))

相关

exp, pow methods

LOG2E property

The base 2 logarithm of e (approximately 1.442).

语法Math.LOG2E

Property of

Math

描述

Because LOG2E is a constant, it is a read-only property of Math.

例子

The following example displays the base 2 logarithm of E: document.write("The base 2 logarithm of E is " + Math.LOG2E)

相关

E, LN2, LN10, LOG10E, PI, SQRT1_2, SQRT2 properties

LOG10E property

The base 10 logarithm of e (approximately 0.434).

语法Math.LOG10E

Property of

Math

描述

Because LOG10E is a constant, it is a read-only property of Math.

例子

The following example displays the base 10 logarithm of E: document.write("The base 10 logarithm of E is " + Math.LOG10E)

相关

E, LN2, LN10, LOG2E, PI, SQRT1_2, SQRT2 properties

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有