JavaScript Instance Tutorial: Queryselectorall () method

Source: Internet
Author: User
Tags object empty query

Article Introduction: whenever a call to Queryselectorall () returns a Staticnodelist object regardless of the number of matched elements, if there is no match, then staticnodelist is empty. Queryselectorall () and Queryselector () are the same as the document and element types.

Queryselectorall () accepts the same two parameters as Queryselecort (), the CSS query and the optional namespace parser, but returns all the matching nodes rather than the individual. This method returns an instance of a new type called staticnodelist.

As the name suggests, Staticnodelist has nodelist all the properties and methods, but its underlying implementation is a snapshot of the collection of elements, rather than a dynamic query that always needs to be renewed against the document. Using staticnodelist eliminates most of the performance problems associated with using NodeList objects.

Whenever a call to Queryselectorall () returns a Staticnodelist object regardless of the number of matched elements, if there is no match, then staticnodelist is empty. Queryselectorall () and Queryselector () are the same as the document and element types. The following are some examples:

Get all images in <div> (same as Getelementsbytaname ("img")
var images = document.getElementById ("Mydiv"). Queryselectorall ("img");

Gets all elements containing the "selected" Class
var selected = Document.queryselectorall (". selected");

Gets <strong> elements in all <p> elements
var strongs = Document.queryselectorall ("P Strong");

The returned Staticnodelist object can be iterated in the same way as NodeList, using item () or a square bracket tag to retrieve a single element. As in the following example:

for (var i = 0, len = strongs.length i < len; i++) {
    var strong = strong[i];//or Strongs.item (i)
    Strong.classna Me = "important";
}

You can use a namespace parser in Queryselectorall () as shown in the following example:

var svgimages = Document.queryselectorall ("Svgsvg", function (prefix) {
    switch (prefix) {return
        "http:// Www.w3.org/2000/svg ";
        Other code here
    }
});

This example retrieves all SVG images in the document that use <svg:svg> definitions.



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.