


CSS Journey-Second Stop How to understand various selectors more deeply_html/css_WEB-ITnose
In the last article we talked about why we should use css. In this article we will start with the selector. Everyone knows that the browser will parse the remote HTML into a dom model. With the dom model , the html will become
in 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 be able to do various getElementById, so when the browser parses it into After 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, that is, it must have a deep understanding of dom. Model.
1: Understanding the Dom model
First, let’s look at the code below.
1 |
|
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 very 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 stripped naked~~~~
1: Child node
Find the child node, the essence There are two types, really only looking for "child nodes", "finding all children (including descendants)"
<1> Descendant selector
First Looking at the HTML below, I think you can easily draw the DOM tree. Then the next question is how to draw all the descendant spans in the body with red.
1 |
|
2. Child selector
<1> ">"How to play
This is me too In the second case, you really only want to find child nodes. It’s very simple in CSS, just use the > symbol. Isn’t it interesting? It’s the same way as jquery, right?
1 |
|
<2> "Pseudo-selector" gameplay
In addition to the above gameplay, it can also be used in CSS3" The "pseudo-selector" gameplay is really 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.
1 |
|
3. Sibling nodes
Sibling nodes are also easy to understand and can be solved by using " " in css. You can see below that I successfully drew the second p in red.
1 |
|
4. Attribute Selector
If you have played with jquery, I want to be very clear about this attribute selector. Let’s look at an example first. , I want to find the p element with name=test and mark it red.
1 |
|
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 point of "you know it" realm.
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 based on the "tag" defined in css Go to the "tag" specified in the dom. 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 on jquery, or we can get a glimpse
2, because the selector usage that can be displayed by css can be done 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.
1 |
|
After some searching in jquery, you can finally see that the native method of queryselectorAll is just called. You can also use the console It is clear that the last
results 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 in 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.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The article discusses the HTML <progress> element, its purpose, styling, and differences from the <meter> element. The main focus is on using <progress> for task completion and <meter> for stati

Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

The article discusses the HTML <datalist> element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

The article discusses the HTML <meter> element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates <meter> from <progress> and ex

The article discusses the viewport meta tag, essential for responsive web design on mobile devices. It explains how proper use ensures optimal content scaling and user interaction, while misuse can lead to design and accessibility issues.

The article discusses the <iframe> tag's purpose in embedding external content into webpages, its common uses, security risks, and alternatives like object tags and APIs.

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.
