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

WBOY
Release: 2016-06-24 11:42:04
Original
1107 people have browsed it

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.

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template