JS Learning Advanced-element acquisition and style setting

Source: Internet
Author: User

var IMGs = document.queryselectorall ("article img");

Gets all the IMG elements of the immediate or indirect descendants of the article element,

<article>        <div>             </div></ Article>

Gets a 2 img element.

Another method, Queryselector (), returns only the first result found.

Other syntax differences for acquiring elements are:

Get article Direct child element: var IMGs = Document.queryselectorall ("article> img");

Gets all the elements immediately following an element: var IMGs = Document.queryselectorall ("img + P");

Gets an element of an empty property: var IMGs = Document.queryselectorall ("img[alt=");

Gets an element for which the property is not empty: var IMGs = Document.queryselectorall ("Img:not ([alt=])");

/*queryselectorall () Gets a collection of elements that are not "dynamic", and if the update is published after the collection is acquired, the updates made by the page are not reflected in the collection, and the getElementById () usually used is more efficient */

CSS style settings (three types):

Modified by the style property of the element: Elem.style.backgroundColor = "Red";

/* Style name "Hump" notation, second word capital */

To modify one or more properties of a single element:

Elem.setattribute ("Style", "background-color:red;  Color:white; BORDER:1PX solid black ");

predefined styles, setting the class property of an element:

1 . stipe{2   background-color:red; 3   Color:white; 4   border:1px solid black;     5 }6.... 7 Elem.setattribute ("Class", "stripe");

For getting an element property value using GetAttribute (),

To access the specific style settings of an element at a particular point in time, it is more complicated and not quite clear, here first an example, for later reference:

var elem = document.getElementById ("test"); var bkcolor = Elem.currentstyle? elem.currentstyle["BackgroundColor"]:            window.getComputedStyle (Elem). GetPropertyValue ("Background-color"); Console.log (Bkcolor);

Can work across browsers, and later contact to re-study it!

JS Learning Advanced-element acquisition and style setting

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.