当前页面: 开发资料首页 → Javascript 专题 → javascript手冊-s1
摘要: javascript
A string beginning with a question mark that specifies any query information in the URL.
1. links[index].search 2. location.search
index is an integer representing a link object.
link, location
The search property specifies a portion of the URL.
You can set the search property at any time, although it is safer to set the href property to change a location. If the search that you specify cannot be found in the current location, you will get an error.
See Section 3.3 of RFC 1738 for complete information about the search.
In the following example, the window.open statement creates a window called newWindow and loads the specified URL into it. The document.write statements display all the properties of newWindow.location in a window called msgWindow.
newWindow=window.open ("http://guide-p.infoseek.com/WW/NS/Titles?qt=RFC+1738+&col=WW") msgWindow.document.write("newWindow.location.href = " + newWindow.location.href + "") msgWindow.document.write("newWindow.location.protocol = " + newWindow.location.protocol + "
") msgWindow.document.write("newWindow.location.host = " + newWindow.location.host + "
") msgWindow.document.write("newWindow.location.hostName = " + newWindow.location.hostName + "
") msgWindow.document.write("newWindow.location.port = " + newWindow.location.port + "
") msgWindow.document.write("newWindow.location.pathname = " + newWindow.location.pathname + "
") msgWindow.document.write("newWindow.location.search = " + newWindow.location.search + "
") msgWindow.document.write("newWindow.location.hash = " + newWindow.location.hash + "
") msgWindow.document.close()
The previous example displays the following output:
newWindow.location.href = http://guide-p.infoseek.com/WW/NS/Titles?qt=RFC+1738+&col=WW newWindow.location.protocol = http: newWindow.location.host = guide-p.infoseek.com newWindow.location.hostName = guide-p.infoseek.com newWindow.location.port = newWindow.location.pathname = /WW/NS/Titles newWindow.location.search = ?qt=RFC+1738+&col=WW newWindow.location.hash =
Selects the input area of the specified password, text, or textarea object.
1. passwordName.select() 2. textName.select() 3. textareaName.select()
passwordName is either the value of the NAME attribute of a password object or an element in the elements array.
textName is either the value of the NAME attribute of a text object or an element in the elements array.
textareaName is either the value of the NAME attribute of a textarea object or an element in the elements array.
password, text, textarea
Use 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.
In 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 the 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">
A selection list or scrolling list on an htm form. A selection list lets the user choose one item from a list. A scrolling list lets the user choose one or more items from a list.
To define a select object, use standard htm 语法 with the addition of the onBlur, onChange, and onFocus event handlers:
<select NAME="" [SIZE="integer"] [MULTIPLE] [onBlur="handlerText"] [onChange="handlerText"] [onFocus="handlerText"]> <option VALUE="" [SELECTED]> textToDisplay [ ... <option> textToDisplay] </select>NAME="selectName" specifies the name of the select object. You can access this value using the name property.
To use a select object's properties and methods:
1. selectName.propertyName 2. selectName.methodName(parameters) 3. formName.elements[index].propertyName 4. formName.elements[index].methodName(parameters)selectName is the value of the NAME attribute of a select object.
To use an option's properties:
1. selectName.options[index1].propertyName 2. formName.elements[index2].options[index1].propertyNameselectName is the value of the NAME attribute of a select object.
A select object on a form looks as follows. The object on the left is a selection list that lets the user choose one item; the object on the right is a scrolling list that lets the user choose one or more items: <form><select name=music_type_single> <option selected>R&B<option>Jazz<option>Blues<option>New Age</option></select> <select multiple name=music_type_multi> <option selected>R&B<option>Jazz<option>Blues<option>New Age</option></select> </form>
A select object is a form element and must be defined within a <form> tag.
You can reference the options of a select object in your code by using the options array. This array contains an entry for each option in a select object (<option> tag) in source order. For example, if a select object named musicStyle contains three options, these options are reflected as musicStyle.options[0], musicStyle.options[1], and musicStyle.options[2].
To use the options array:
1. selectName.options 2. selectName.options[index] 3. selectName.options.length
selectName is either the value of the NAME attribute of a select object or an element in the elements array.
index is an integer representing an option in a select object.
To obtain the number of options in a select object, use the length property of either the options array or the select object:
1. selectName.length 2. selectName.options.length
The select object has properties that you can access only through the options array. These properties are listed below.
Even though each element in the options array represents a select option, the value of options[index] is always null. The value returned by selectName.options represents the full htm statement for the selectName object.
Elements in the options array are read-only. For example, the statement selectName.options[0]="guitar" has no effect.
The select object has the following properties:
The options array has the following properties:
Example 1. The following example displays a selection list and a scrolling list.
Choose the music types for your free CDs:
<select NAME="music_type_multi" MULTIPLE>
<option SELECTED> R&B
<option> Jazz
<option> Blues
<option> New Age
</select>
Example 2. The following example displays two selection lists that let the user choose a month and day. These selection lists are initialized to the current date. The user can change the month and day by using the selection lists or by choosing preset dates from radio buttons. Text fields on the form display the values of the select object's properties and indicate the date chosen and whether it is Cinco de Mayo.
Choose a month and day: Month: <select NAME="monthSelection" onChange="updatePropertyDisplay(this,document.selectForm.daySelection)"> <option> January <option> February <option> March <option> April <option> May <option> June <option> July <option> August <option> September <option> October <option> November <option> December </select> Day: <select NAME="daySelection" onChange="updatePropertyDisplay(document.selectForm.monthSelection,this)"> <option> 1 <option> 2 <option> 3 <option> 4 <option> 5 <option> 6 <option> 7 <option> 8 <option> 9 <option> 10 <option> 11 <option> 12 <option> 13 <option> 14 <option> 15 <option> 16 <option> 17 <option> 18 <option> 19 <option> 20 <option> 21 <option> 22 <option> 23 <option> 24 <option> 25 <option> 26 <option> 27 <option> 28 <option> 29 <option> 30 <option> 31 </select>
Set the date to: <input TYPE="radio" NAME="dateChoice" onClick=" monthSelection.selectedIndex=0; daySelection.selectedIndex=0; updatePropertyDisplay(document.selectForm.monthSelection,document.selectForm.daySelection)"> New Year's Day <input TYPE="radio" NAME="dateChoice" onClick=" monthSelection.selectedIndex=4; daySelection.selectedIndex=4; updatePropertyDisplay(document.selectForm.monthSelection,document.selectForm.daySelection)"> Cinco de Mayo <input TYPE="radio" NAME="dateChoice" onClick=" monthSelection.selectedIndex=5; daySelection.selectedIndex=20; updatePropertyDisplay(document.selectForm.monthSelection,document.selectForm.daySelection)"> Summer Solstice
Property values:
Date chosen: <input TYPE="text" NAME="textFullDate" VALUE="" SIZE=20">
monthSelection.length<input TYPE="text" NAME="textMonthLength" VALUE="" SIZE=20">
daySelection.length<input TYPE="text" NAME="textDayLength" VALUE="" SIZE=20">
monthSelection.name<input TYPE="text" NAME="textMonthName" VALUE="" SIZE=20">
daySelection.name<input TYPE="text" NAME="textDayName" VALUE="" SIZE=20">
monthSelection.selectedIndex<input TYPE="text" NAME="textMonthIndex" VALUE="" SIZE=20">
daySelection.selectedIndex<input TYPE="text" NAME="textDayIndex" VALUE="" SIZE=20">
Is it Cinco de Mayo? <input TYPE="text" NAME="textCinco" VALUE="" SIZE=20">
document.selectForm.monthSelection.selectedIndex=today.getMonth()
document.selectForm.daySelection.selectedIndex=today.getDate()-1
updatePropertyDisplay(document.selectForm.monthSelection,document.selectForm.daySelection)
</form>
</body>
相关 the 例子 for the defaultSelected property.
A Boolean value specifying the current selection state of an option in a select object.
selectName.options[index].selected
selectName is either the value of the NAME attribute of a select object or an element in the elements array.
index is an integer representing an option in a select object.
options array
If an option in a select object is selected, the value of its selected property is true; otherwise, it is false.
You can set the selected property at any time. The display of the select object updates immediately when you set the selected property.
In general, the selected property is more useful than the selectedIndex property for select objects that are created with the MULTIPLE attribute. With the selected property, you can evaluate every option in the options array to determine multiple selections, and you can select individual options without clearing the selection of other options.
See the 例子 for the defaultSelected property.
An integer specifying the index of the selected option in a select object.
1. selectName.selectedIndex 2. selectName.options.selectedIndex
selectName is either the value of the NAME attribute of a select object or an element in the elements array.
Options in a select object are indexed in the order in which they are defined, starting with an index of 0. You can set the selectedIndex property at any time. The display of the select object updates immediately when you set the selectedIndex property. Both forms of the 语法 specify the same value.
In general, the selectedIndex property is more useful for select objects that are created without the MULTIPLE attribute. If you evaluate selectedIndex when multiple options are selected, the selectedIndex property specifies the index of the first option only. Setting selectedIndex clears any other options that are selected in the select object.
The selected property of the select object's options array is more useful for select objects that are created with the MULTIPLE attribute. With the selected property, you can evaluate every option in the options array to determine multiple selections, and you can select individual options without clearing the selection of other options.
In the following example, the getSelectedIndex() function returns the selected index in the musicType select object:
function getSelectedIndex() { return document.musicForm.musicType.selectedIndex }The previous example assumes that the select object is similar to the following:
<select NAME="musicType"> <option SELECTED> R&B <option> Jazz <option> Blues <option> New Age </select>
The self property is a synonym for the current window or frame.
1. self.propertyName 2. self.methodName
propertyName is the defaultStatus, status, length, or name property when self refers to a window object.
propertyName is the length or name property when self refers to a frame object.
methodName is any method associated with the window object.
frame, window
The self property refers to the current window or frame.
Use the self property to disambiguate a window property from a form or form element of the same name. You can also use the self property to make your code more readable.
The self property is read-only. The value of the self property is
where nameAttribute is the NAME attribute if self refers to a frame, or an internal reference if self refers to a window.
In the following example, self.status is used to set the status property of the current window. This usage disambiguates the status property of the current window from a form or form element called "status" within the current window.
Sets the day of the month for a specified date.
dateObjectName.setDate(dayValue)
dateObjectName is either the name of a date object or a property of an existing object.
dayValue is an integer from 1 to 31 or a property of an existing object, representing the day of the month.
Date
The 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") theBigDay.setDate(24)
Sets the hours for a specified date.
dateObjectName.setHours(hoursValue)
dateObjectName is either the name of a date object or a property of an existing object.
hoursValue is an integer between 0 and 23 or a property of an existing object, representing the hour.
Date
theBigDay.setHours(7)
Sets the minutes for a specified date.
dateObjectName.setMinutes(minutesValue)
dateObjectName is either the name of a date object or a property of an existing object.
minutesValue is an integer between 0 and 59 or a property of an existing object, representing the minutes.
Date
theBigDay.setMinutes(45)
Sets the month for a specified date.
dateObjectName.setMonth(monthValue)
dateObjectName is either the name of a date object or a property of an existing object.
monthValue is an integer between 0 and 11 (representing the months January through December), or a property of an existing object.
Date
theBigDay.setMonth(6)
Sets the seconds for a specified date.
dateObjectName.setSeconds(secondsValue)
dateObjectName is either the name of a date object or a property of an existing object.
secondsValue is an integer between 0 and 59 or a property of an existing object.
Date
theBigDay.setSeconds(30)