Introduction to common objects in document and event javascript [from]: Taipa
[Note: The element name attribute on the page and the name referenced by JavaScript must be case-sensitive. Otherwise, an error message "the referenced element is null or not an object" is displayed.]
========================================================== ======================================
Object Property:
Document. title // set the document title is equivalent to the <title> tag of HTML.
Document. bgColor // set the background color of the page
Document. fgColor // set the foreground color (text color)
Document. linkColor // The link color that has not been clicked
Document. alinkColor // the color of the activation Link (focus on this link)
Document. vlinkColor // the color of the clicked Link
Document. URL // set the URL attribute to open another webpage in the same window
Document. fileCreatedDate // file creation date, read-only attribute
Document. fileModifiedDate // file modification date, read-only attribute
Document. fileSize // file size, read-only attribute
Document. cookie // set and read cookies
Document. charset // set the character set to simplified Chinese: gb2312
----------
Common Object methods:
Document. write () // dynamically write content to the page
Document. createElement (Tag) // create an html Tag object
Document. getElementById (ID) // get the object with the specified ID value
Document. getElementsByName (Name) // get the object with the specified Name value
Document. body. appendChild (oTag)
========================================================== ======================================
Body-subject sub-Object
Document. body // specify the beginning and end of the document body, which is equivalent to <body> </body>
Document. body. bgColor // set or obtain the background color behind the object
Document. body. link // the color of the link that has not been clicked
Document. body. alink // the color of the activation Link (focus on this link)
Document. body. vlink // the color of the clicked Link
Document. body. text // text color
Document. body. innerText // set <body>... </Body> text
Document. body. innerHTML // set <body>... </Body> HTML code
Document. body. topMargin // top margin of the page
Document. body. leftMargin // left margin of the page
Document. body. rightMargin // right margin of the page
Document. body. bottomMargin // bottom margin of the page
Document. body. background // background image
Document. body. appendChild (oTag) // dynamically generate an HTML Object
----------
Common Object events:
Document. body. onclick = "func ()" // The mouse pointer clicks the object to be triggered
Document. body. onmouseover = "func ()" // triggered when the mouse pointer is moved to an object
Document. body. onmouseout = "func ()" // triggered when the mouse pointer is removed from the object
========================================================== ======================================
Location-location sub-object:
Part after document. location. hash // #
Document. location. host // domain name + port number
Document. location. hostname // Domain Name
Document. location. href // complete URL
Document. location. pathname // directory
Document. location. port // port number
Document. location. protocol // network protocol (http :)
Document. location. search //? Part after
----------
Common Object events:
Using eny. location. reload () // refresh the webpage
Document. location. reload (URL) // open a new webpage
Document. location. assign (URL) // open a new webpage
Document. location. replace (URL) // open a new webpage
========================================================== ======================================
Selection-selection Sub-Object
Document. selection
========================================================== ======================================
Images set (images in the page ):
----------
A) reference through a set
Document. images // The label on the corresponding page
Document. images. length // Number of labels on the corresponding page
Document. images [0] // 1st tags
Document. images [I] // The I-1 label
----------
B) direct reference through the nane attribute
Document. images. oImage // document. images. name attribute
----------
C) reference the image's src attribute
Document. images. oImage. src // document. images. name attribute. src
----------
D) create an image
Var oImage
OImage = new Image ()
Document.images.oimage.src000000001.jpg"
At the same time, you can create a tag on the page to display it.
----------
Sample Code (dynamic image creation ):
<Html>
<Script language = "javascript">
Var oImage
OImage = new Image ()
Document.images.oimage.src000000001.jpg"
</Script>
</Html>
<Html>
<Script language = "javascript">
OImage = document. caeateElement ("IMG ")
Oimage.src000000001.jpg"
Document. body. appendChild (oImage)
</Script>
</Html>
========================================================== ======================================
Forms set (forms in the page ):
----------
A) reference through a set
Document. forms // The <form> tag on the corresponding page
Document. forms. length // Number of <form> labels on the corresponding page
Document. forms [0] // 1st <form> tags
Document. forms [I] // The I-1 <form> label
Document. forms [I]. length // number of controls in the I-1 <form>
Document. forms [I]. elements [j] // The I-1 control in the J-1 <form>
----------
B) direct reference through the label name attribute
<Form name = "Myform"> <input name = "myctrl"> </form>
Document. Myform. myctrl // document. form name. Control name
----------
C) Access Form attributes
Document. forms [I]. name // corresponds to the <form name> attribute
Document. forms [I]. action // corresponds to the <form action> attribute
Document. forms [I]. encoding // corresponds to the <form enctype> attribute
Document.formspolici2.16.tar get // corresponding to the <form target> attribute
Document. forms [I]. appendChild (oTag) // dynamically insert a control
----------
Sample Code (form ):
<Html>
<! -Script related to the Text control->
<Form name = "Myform">
<Input type = "text" name = "oText">
<Input type = "password" name = "oPswd">
<Form>
<Script language = "javascript">
// Obtain the value of the text password box
Document. write (document. Myform. oText. value)
Document. write (document. Myform. oPswd. value)
</Script>
</Html>
----------
Sample Code (checkbox ):
<Html>
<! -Checkbox and radio control->
<Form name = "Myform">
<Input type = "checkbox" name = "chk" value = "1"> 1
<Input type = "checkbox" name = "chk" value = "2"> 2
</Form>
<Script language = "javascript">
Function fun (){
// Traverse the value of the checkbox control and determine whether to select
Var length
Length = document. forms [0]. chk. length
For (I = 0; I <length; I ++ ){
V = document. forms [0]. chk [I]. value
B = document. forms [0]. chk [I]. checked
If (B)
Alert (v = v + "selected ")
Else
Alert (v = v + "unselected ")
}
}
</Script>
<A href = # onclick = "fun ()"> ddd </a>
</Html>
----------
Sample Code (Select ):
<Html>
<! -Script related to the Select control->
<Form name = "Myform">
<Select name = "oSelect">
<Option value = "1"> 1 </option>
<Option value = "2"> 2 </option>
<Option value = "3"> 3 </option>
</Select>
</Form>
<Script language = "javascript">
// Traverse the option items of the select Control
Var length
Length = document. Myform. oSelect. length
For (I = 0; I <length; I ++)
Document. write (document. Myform. oSelect [I]. value)
</Script>
<Script language = "javascript">
// Traverse the option and determine whether an option is selected
For (I = 0; I <document. Myform. oSelect. length; I ++ ){
If (document. Myform. oSelect [I]. selected! = True)
Document. write (document. Myform. oSelect [I]. value)
Else
Document. write ("<font color = red>" + document. Myform. oSelect [I]. value + "</font> ")
}
</Script>
<Script language = "javascript">
// Print the selected option based on SelectedIndex
// (0 to document. Myform. oSelect. length-1)
I = document. Myform. oSelect. selectedIndex
Document. write (document. Myform. oSelect [I]. value)
</Script>
<Script language = "javascript">
// Dynamically Add the option item of the select Control
Var oOption = document. createElement ("OPTION ");
OOption. text = "4 ″;
OOption. value = "4 ″;
Document. Myform. oSelect. add (oOption );
</Script>
<Html>
========================================================== ======================================
Div set (layer in the page ):
<Div id = "oDiv"> Text </Div>
Document. all. oDiv // reference layer oDiv
Document. all. oDiv. style. display = "" // set the layer to visible.
Document. all. oDiv. style. display = "none" // The layers are hidden.
Document. getElementId ("oDiv") // reference an object through getElementId
Document. getElementId ("oDiv"). style = ""
Document. getElementId ("oDiv"). display = "none"
/* Document. all indicates the set of all objects in the document.
Only ie supports this attribute, so it is also used to determine the browser type */
----------
Four attributes of a layer object
Document. getElementById ("ID"). innerText // dynamic output text
Document. getElementById ("ID"). innerHTML // dynamically output HTML
Document. getElementById ("ID"). outerText // same as innerText
Document. getElementById ("ID"). outerHTML // same as innerHTML
----------
Sample Code:
<Html>
<Script language = "javascript">
Function change (){
Document. all. oDiv. style. display = "none"
}
</Script>
<Div id = "oDiv" onclick = "change ()"> Text </Div>
</Html>
<Html>
<Script language = "javascript">
Function changeText (){
Document. getElementById ("oDiv"). innerText = "NewText"
}
</Script>
<Div id = "oDiv" onmouseover = "changeText ()"> Text </Div>
</Html>
Common event objects in javascript
The Javascript Event object is used to describe Javascript events. It mainly applies to various browser versions later than IE4 and nn4.
Event indicates the Event status, such as the Event element, keyboard status, mouse position, and mouse button status. Once an Event occurs, an event object is generated. If you click a button, the corresponding Event object is generated in the browser memory.
References to events by IE
In IE4 or later versions, the event object is accessed as the window property: window. envent. The referenced window part is optional. Therefore, the script treats the event object like a global reference:
Event. propertyName
Evnet object member table:
The following table lists the members of the event object. Click the label on the left to select the member type you want to view.
Object
DataTransfer provides access to the predefined clipboard format for use in the drag operation.
Set
Bookmarks returns a set of ActiveX Data Objects (ADO) bookmarks bound to the rows affected by the current event.
BoundElements returns a set of all elements bound to a given dataset.
Attribute
Abstract: Use the event object to obtain the Abstract content of the project banner in the advanced Stream Redirector (ASX) file.
AltKey sets or obtains the status of the Alt key.
AltLeft sets or obtains the status of the Left Alt key.
Banner uses the event object to obtain the Banner content of the item in the advanced Stream Redirector (ASX) file.
Click to set or obtain the mouse button that the user presses.
CancelBubble sets or obtains whether the current event needs to bubble up In the event handle.
ClientX sets or obtains the x coordinates of the mouse pointer position relative to the client area of the window. The client area does not include the control and scroll bar of the window.
ClientY sets or obtains the y coordinate of the mouse pointer position relative to the client area of the window. The client area does not include the control and scroll bar of the window.
The retrieval of contentOverflow indicates whether the document contains additional content after processing the current LayoutRect object.
CtrlKey sets or obtains the status of the Ctrl key.
CtrlLeft sets or obtains the status of the Left Ctrl key.
Dataworks sets or obtains the data columns affected by the oncellchange event.
FromElement sets or gets the object that is activated when an event occurs or the mouse is about to exit.
Set or obtain the Unicode key code associated with the buttons that cause the event.
MoreInfo obtains the MoreInfo content of the project banner in the advanced Stream Redirector (ASX) file through the event object.
Obtain the position of the next page in the print template from nextPage.
OffsetX is used to set or obtain the x coordinate between the cursor position and the object that triggers the event.
OffsetY is used to set or obtain the y coordinate of the object relative to the trigger event.
PropertyName: set or obtain the property name that has been changed on the object.
Qualifier sets or gets the name of the data member provided by the data source object.
Reason sets or obtains the data transmission result of the data source object.
Recordset sets data source objects or obtains references to default datasets.
Repeat checks whether the onkeydown event is repeated.
ReturnValue is used to set or obtain the return value of an event.
SaveType obtains the clipboard type when oncontentsave is triggered.
ScreenX is used to set or obtain the x coordinates of the cursor position relative to the user's screen.
ScreenY is used to set or obtain the y coordinate of the mouse pointer position relative to the user's screen.
ShiftKey sets or obtains the status of the Shift key.
ShiftLeft sets or obtains the status of the Left Shift key.
SrcElement sets or gets the object that triggers the event.
SrcFilter sets or gets the filter object that triggers the onfilterchange event.
SrcUrn obtains the unified Resource Name (URN) for triggering event behavior ).
Set or retrieve the reference of the object to which the user wants to move the mouse pointer.
Type obtains the event name from the event object.
WheelDelta sets or gets the scroll distance and direction of the scroll wheel button.
X sets or obtains the x-pixel coordinates of the cursor position relative to the parent document.
Y sets or obtains the y pixel coordinate of the cursor position relative to the parent document.
Note
The event object is only available in the event -- this means that you can use this object in the event handle, but not in other code.
Although all event attributes can be accessed through all event objects, some attributes may be meaningless in some events. For example, the fromElement and toElement attributes make sense only when processing onmouseover and onmouseout events.
In Microsoft Visual Basic script version (VBScript), you must access the event object through the window object.
This object is available in Microsoft Internet Explorer 4.0 scripts