Method Code for using javascript to reference objects _ javascript skills

Source: Internet
Author: User
Javascript reference object method code reference method on the same page:

1. Use id:

Link1.href

2. Use name:

Document. all. link1.href

3. Use sourseIndex:

Document. all (4). href // note that there are HTML, HEAD, TITLE, and BODY in front, so it is 4

4. Use a link set:

Document. anchors (0 ). href // all sets include all, anchors, applets, areas, attributes, behaviorUrns, bookmarks, boundElements, cells, childNodes, children, controlRange, elements, embeds, filters, forms, frames, images, imports, links, mimeTypes, options, plugins, rows, rules, scripts, styleSheets, tBodies, TextRectangle, please refer to the MSDN introduction. In fact, method 3 and method 4 use the same set, but one is all, which can include all the tags on the page, while anchors only includes links.

5. getElementById:

Document. getElementById ("link1"). href

6. getElementsByName:

Document. getElementsByName ("link1") [0]. href // This is also a set of all tags whose names are equal to the parameters included in the Method

7. getElementsByTagName:

Document. getElementsByTagName ("A") [0]. href // This is also A set of all tags whose names are equal to the parameters included in the Method

8. tags set:

Document. all. tags ("A") [0]. href // same as Method 7, get A set by Tag Name

In addition, event. scrElement can get the reference of the trigger time mark; document. elementFromPoint (x, y) can be used to obtain references to elements in the x and y coordinates. document. body. componentFromPoint (event. clientX, event. clientY) to get the reference of the element where the mouse is located. It can also be referenced by the relationship between the parent and child nodes of the element and the sibling nodes, such as nextSibling (the last node of the current node) previussibling (the former node of the current node), childNodes, children, firstChild, lastChild, and parentElement are all references of parent and child nodes and sibling nodes.

The above is a common reference method on the same page, and also involves

==================
For frame splitting pages, you can use parent. frames ("frame name"), top. frames ("frame name") is used to reference different frames. Subsequent references are the same as those in the same page, and multiple parents are also supported.
For example:
Parent. frames ("frame1" 2.16.doc ument. all. link1
Top. frames ("frame1" 2.16.doc ument. all. link1

==================
For Windows. open () window, you can use var newwin = window. open (), and then use newwin to reference the new window. The references behind the new window are the same as those on the same page. opener to reference and open its window. You can write opener briefly, for example:
Var newwin = window. open ()
Parent window (here is the window using the window. open () method ):
Newwin.doc ument. all. link1 // The parent window can reference objects in the new window.
Subwindow (window. open ):
Opener.doc ument. all. link1 // The subwindow can reference the parent window object.

Multiple openers are also supported, such as opener.opener.doc ument. all. link1.


There are a variety of methods, and sometimes you need to choose based on the specific situation, flexible use can be easily used.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.