Absolute unique reference method for Web page elements

Source: Internet
Author: User
Tags eval reference unique id
web| page

We know that the easiest way to reference an HTML element in a Web page is to use the getElementById method, or a similar set of GETELEMENTSBYXXX methods. However, it should be noted that none of these methods can really uniquely determine the reference to an element.

As for getelementsbyxxx we do not say, it itself is getelements, very clear, then getElementById can not only determine an element? Of course it is, no. Because of the strictness of the HTML, the ID of the duplicate element on the same page is allowed. For more details on IDs, refer to the distinction between the ID of the HTML element and the name attribute, a text.

So how can we uniquely determine the reference to an element? Use a reference structure between the DOM to find it? Although this method is OK, but operability is too bad. Here's the HTML attribute UniqueID that I've introduced, which is a proprietary attribute of Microsoft's expansion of HTML elements, and does not conform to the rules of the consortium. This UniqueID has some undocumented features, and UniqueID is actually a set of element representations similar to ID, but it is a pure client-side labeling scheme. Some of its details have already been introduced in the previous link, and we'll talk about its features here.

    because it is the representation system of an element, is it also due to the same characteristics as the ID? In fact, in addition to the UniqueID generation and ID completely different, UniqueID other performance and ID is the same. We can refer to the element directly through this UniqueID, just like the ID, only because the UniqueID is dynamically generated, so we can only use this uniqueid dynamically, not in literal form. The following is a use example:
var g_elements = {};
var span = document.createelement (' span ');
Document.body.appendChild (span);
G_elements[span.uniqueid] = span;
    so we cache spans through UniqueID, and we have to reference this span, so we can:
var elements = g_elements. Getexpandokeys ();
for (Var i=0 i < elements.length; ++i)
{
    var element = eval (elmenets[i]);
   //Todo:other logic
}
    In addition to using Eval to reference this UniqueID, getElementById also has a undocumented feature, which is that it also supports UniqueID, so that we can use getElementById (Elements[i]) to refer to a page element with absolute uniqueness.

Some people may wonder, what is the use of this UniqueID? In fact, in the dynamic generation of a large number of page elements, the use of UniqueID can be very aspects of the element to generate a real unique ID, it is the significance of these dynamic elements can be cache up, do unified management. Especially in the implementation of the dynamic in the TreeView, if the reasonable use of the benefits of the advent of UniqueID, you can avoid a lot of the need for repeated recursion to the tree to complete the work. Also, UniqueID is the native solution provided by the DOM of IE, which is much more efficient than it is to make a unique mark, and facilitates the retrieval of elements.

Reference information: Getexpandokeys (), in addition, this article is not suitable for Firefox and other non-ie kernel browsers.



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.