Home Web Front-end HTML Tutorial CSS Journey (2): How to understand various selectors more deeply_html/css_WEB-ITnose

CSS Journey (2): How to understand various selectors more deeply_html/css_WEB-ITnose

Jun 24, 2016 am 11:42 AM

Original source: First-line coder



In the last article we talked about why we should use css. In this article we will start with the selector. Everyone knows The browser will parse the remote HTML into a dom model. With the dom model, the html will become an xml format. Otherwise, it will be a bunch of "messy" strings. In this case, no one will know what it is, and js will not There is no way to getElementById, so when the browser parses it into a dom structure, the browser will easily find the corresponding position in the dom structure according to the selectors of various CSS rules. Then the next problem will naturally be serious, then It is necessary to understand the dom model in depth.

1: Understanding the Dom model

First we look at the code below.

<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head>    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />    <title></title></head><body>    <p>有名的公司一栏</p>    <hr />    <ul>        <li>百度</li>        <li>新浪</li>        <li>阿里</li>    </ul></body></html>
Copy after login

Using this code we can easily draw the dom tree.

When you see this DOM tree, do you suddenly feel that the amount of information is particularly large? It is very simple, because it is a tree, so there are some trees Characteristics, such as "child node", "father node",

"sibling node", "first left child", "last left child", etc., correspond to the various things I will talk about later. In this case, let’s see if it feels good to see the html being stripped naked~~~~

1: Child node

Finding child node, essentially there are two types, Really only find "child nodes", "find all children (including descendants)"

<1> Descendant Selector

First look at the html below, I think you can draw it easily Now that the DOM tree is out, the next question is how to paint all descendant spans in the body with red.

<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head>    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />    <title></title>    <style type="text/css">        body span {            color: red;        }    </style></head><body>    <span>我是span1</span>    <ul>        <li>            <ul><span>我是span2</span></ul>        </li>    </ul></body></html>
Copy after login

2. Child selector

<1> ”>” How to play

This is also the second situation I mentioned , it really only looks for child nodes. It’s very simple in CSS, just use the > sign. Isn’t it interesting? It’s the same way as jquery, right?

<html xmlns="http://www.w3.org/1999/xhtml"><head>    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />    <title></title>    <style type="text/css">        body > span {            color: red;        }    </style></head><body>    <span>我是span1</span>    <ul>        <li>            <ul><span>我是span2</span></ul>        </li>    </ul></body></html>
Copy after login

<2> "Pseudo-selector" gameplay

In addition to the above gameplay, you can also use the "pseudo-selector" gameplay in CSS3. It’s so powerful. The next article will explain it specifically. Here I only introduce one: nth-child usage. If

you have played with jquery, nothing will be a problem.

<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head>    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />    <title></title>    <style type="text/css">        body > span:nth-child(1) {            color: red;        }    </style></head><body>    <span>我是span1</span>    <span>我是span2</span>    <ul>        <li>            <ul><span>我是span3</span></ul>        </li>    </ul></body></html>
Copy after login

3. Sibling nodes

Sibling nodes are also easy to understand. It can be solved by using " " in css. You can see that I succeeded below Draw the second p in red.

<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head>    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />    <title></title>    <style type="text/css">        .test + p {            color:red;        }    </style></head><body>    <p class="test">我是第一个段落</p>    <p>我是第二个段落</p></body></html>
Copy after login

4. Attribute selector

If you have played with jquery, I want to be very clear about this attribute selector. First, look at an example. I want to find name= Mark the p element of test in red.

<html xmlns="http://www.w3.org/1999/xhtml"><head>    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />    <title></title>    <style type="text/css">        p[name='test'] {            color: red;        }    </style>    <script src="Scripts/jquery-1.10.2.js"></script></head><body>    <p name="test">我是第一个段落</p>    <p>我是第二个段落</p></body></html>
Copy after login

Up to now, do you feel that the gameplay is exactly the same as jquery, and the feeling is getting stronger and stronger, and it has reached the realm of "you understand".

2: Speculation on the internal mechanism of CSS

As mentioned at the beginning of the article, the browser will apply the style of this tag to the "tag" specified in the dom based on the "tag" defined in the css. tag ", for example, I defined a

p style in CSS, but how can the browser find all the p elements in the dom? ? ? Because of the closed source, we cannot know its internal mechanism, but in jquery, we may have a glimpse

2, because the selector usage that can be displayed by css can be found in jquery. Then I can't wait to see how jquery extracts my various selector writing methods. Let's take a look at the source code.

<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head>    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />    <title></title>    <style type="text/css">        p[name='test'] {            color: red;        }    </style>    <script src="Scripts/jquery-1.10.2.js"></script>    <script type="text/javascript">        $(document).ready(function () {            $("p[name='test']").hide();        });    </script></head><body>    <p name="test">我是第一个段落</p>    <p>我是第二个段落</p></body></html>
Copy after login

After searching in jquery, you can finally see that the native method of queryselectorAll is just called. You can also clearly see it in the console. Finally, The results of

are the found p elements. To verify, I opened a console under the taobao page.

Up to now, I have a rough guess, maybe at least under the chrome browser, the browser may also call the queryselectAll method in order to find the specified element in the dom. . .

Okay, that’s about it. Understanding the DOM model is the key, so that we can understand the subsequent rendering behavior of the browser.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Is HTML easy to learn for beginners? Is HTML easy to learn for beginners? Apr 07, 2025 am 12:11 AM

HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

What is an example of a starting tag in HTML? What is an example of a starting tag in HTML? Apr 06, 2025 am 12:04 AM

AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Apr 04, 2025 pm 11:54 PM

GiteePages static website deployment failed: 404 error troubleshooting and resolution when using Gitee...

How to implement adaptive layout of Y-axis position in web annotation? How to implement adaptive layout of Y-axis position in web annotation? Apr 04, 2025 pm 11:30 PM

The Y-axis position adaptive algorithm for web annotation function This article will explore how to implement annotation functions similar to Word documents, especially how to deal with the interval between annotations...

How to use CSS3 and JavaScript to achieve the effect of scattering and enlarging the surrounding pictures after clicking? How to use CSS3 and JavaScript to achieve the effect of scattering and enlarging the surrounding pictures after clicking? Apr 05, 2025 am 06:15 AM

To achieve the effect of scattering and enlarging the surrounding images after clicking on the image, many web designs need to achieve an interactive effect: click on a certain image to make the surrounding...

Why do you need to call Vue.use(VueRouter) in the index.js file under the router folder? Why do you need to call Vue.use(VueRouter) in the index.js file under the router folder? Apr 05, 2025 pm 01:03 PM

The necessity of registering VueRouter in the index.js file under the router folder When developing Vue applications, you often encounter problems with routing configuration. Special...

See all articles