Home  >  Article  >  Web Front-end  >  CSS Journey-Second Stop How to understand various selectors more deeply_html/css_WEB-ITnose

CSS Journey-Second Stop How to understand various selectors more deeply_html/css_WEB-ITnose

WBOY
WBOYOriginal
2016-06-24 11:45:00968browse

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 <!DOCTYPE html> 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 <head> 4     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 5     <title></title> 6 </head> 7 <body> 8     <p>有名的公司一栏</p> 9     <hr />10     <ul>11         <li>百度</li>12         <li>新浪</li>13         <li>阿里</li>14     </ul>15 </body>16 </html>

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)"

f35d6e602fd7d0f0edfa6f7d103c1b57 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 <!DOCTYPE html> 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 <head> 4     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 5     <title></title> 6     <style type="text/css"> 7         body span { 8             color: red; 9         }10     </style>11 </head>12 <body>13     <span>我是span1</span>14     <ul>15         <li>16             <ul><span>我是span2</span></ul>17         </li>18     </ul>19 </body>20 </html>

2. Child selector

f35d6e602fd7d0f0edfa6f7d103c1b57 ">"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 <html xmlns="http://www.w3.org/1999/xhtml"> 2 <head> 3     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 4     <title></title> 5     <style type="text/css"> 6         body > span { 7             color: red; 8         } 9     </style>10 </head>11 <body>12     <span>我是span1</span>13     <ul>14         <li>15             <ul><span>我是span2</span></ul>16         </li>17     </ul>18 </body>19 </html>

2cc198a1d5eb0d3eb508d858c9f5cbdb "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 <!DOCTYPE html> 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 <head> 4     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 5     <title></title> 6     <style type="text/css"> 7         body > span:nth-child(1) { 8             color: red; 9         }10     </style>11 </head>12 <body>13     <span>我是span1</span>14     <span>我是span2</span>15     <ul>16         <li>17             <ul><span>我是span3</span></ul>18         </li>19     </ul>20 </body>21 </html>

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 <!DOCTYPE html> 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 <head> 4     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 5     <title></title> 6     <style type="text/css"> 7         .test + p { 8             color:red; 9         }10     </style>11 </head>12 <body>13     <p class="test">我是第一个段落</p>14     <p>我是第二个段落</p>15 16 </body>17 </html>

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 <html xmlns="http://www.w3.org/1999/xhtml"> 2 <head> 3     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 4     <title></title> 5     <style type="text/css"> 6         p[name='test'] { 7             color: red; 8         } 9     </style>10     <script src="Scripts/jquery-1.10.2.js"></script>11 </head>12 <body>13     <p name="test">我是第一个段落</p>14     <p>我是第二个段落</p>15 </body>16 </html>

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 <!DOCTYPE html> 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 <head> 4     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 5     <title></title> 6  7  8     <style type="text/css"> 9         p[name='test'] {10             color: red;11         }12     </style>13 14     <script src="Scripts/jquery-1.10.2.js"></script>15     <script type="text/javascript">16 17         $(document).ready(function () {18 19  $("p[name='test']").hide(); 20         });21     </script>22 </head>23 <body>24     <p name="test">我是第一个段落</p>25     <p>我是第二个段落</p>26 </body>27 </html>

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.

Statement:
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