<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">

    <channel>
        <title>黄琦雲的博客</title>
        <link>https://knightyun.github.io</link>
        <description>Material theme based on <a href="http://materializecss.com">Materialize.css</a> for jekyll sites
</description>
        <lastBuildDate>Thu, 20 Jun 2019 18:51:13 +0000</lastBuildDate>
        
        <item>
            <title>个人博客网站添加文章搜索功能</title>
            <link>
                https://knightyun.github.io/2019/03/04/articles-search
            </link>
            <description>
                &lt;p&gt;现在很多网站页面里都有搜索模块，包括在线搜索、站内搜索等等，尤其是博客类网站，文章搜索功能就显得比较重要，现在以个人博客网站为例，详细介绍如何给页面添加搜索功能模块，至于如何搭建个人博客网站，可以参考这篇文章：&lt;a href=&quot;https://knightyun.github.io/2018/04/01/github-pages-blog&quot;&gt;https://knightyun.github.io/2018/04/01/github-pages-blog&lt;/a&gt;；&lt;/p&gt;

&lt;h4 id=&quot;功能分析&quot;&gt;功能分析&lt;/h4&gt;
&lt;p&gt;为网页添加搜索模块的第三方网站有不少，实现效果也都不错，既然人家都能通过代码实现此功能，与其过度依赖第三方，何不自己研究一下原理自己实现呢&lt;em&gt;_&lt;/em&gt;；&lt;/p&gt;

&lt;p&gt;网页内搜索功能无非就是在文本输入框内输入一串想要搜索的关键字符串，点击搜索按钮后查找匹配出站内所有文章的匹配结果进行输出，再附上一个文章的链接；针对这个功能，作者本人第一时间想到的则是前端领域里的 &lt;strong&gt;Ajax&lt;/strong&gt;，利用 &lt;strong&gt;XMLHttpRequest()&lt;/strong&gt; 实现数据的交互，不清楚的可以自行百度快速补习，然后一般博客类网站都有 &lt;strong&gt;RSS&lt;/strong&gt; 订阅功能及其数据页面，不清楚的也可以迅速百度了解一下，其实就是站内某个目录内存在一个叫 &lt;strong&gt;feed.xml&lt;/strong&gt; 或者类似名字的页面，XML格式的，早期用于实现新闻内容订阅的，现在用的也比较广泛，里面存储的就是文章相关的一些数据，因此我们就可以获取这个文件里面的所有内容然后实现搜索匹配；&lt;/p&gt;

&lt;p&gt;如果像我一样用 &lt;strong&gt;Gihub Pages&lt;/strong&gt; 搭建的个人博客网站的话，无论用的 &lt;strong&gt;jekyll&lt;/strong&gt; 或者 &lt;strong&gt;hexo&lt;/strong&gt;，一般这个 feed.xml 文件就位于 &lt;strong&gt;根目录&lt;/strong&gt; 下，并且该文件同时记录着某一篇文章所对应的 &lt;strong&gt;标题、页面链接、文章内容&lt;/strong&gt; 等内容，这三个部分后面将用到，xml 内容大致如下：&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.loli.net/2019/03/04/5c7d2c5c9a6ee.png&quot; alt=&quot;rss&quot; /&gt;&lt;/p&gt;

&lt;p&gt;当然有其他更好的实现方法可以自行参考或者在文末评论，那么接下来便通过这个方法来一步步实现搜素功能；&lt;/p&gt;

&lt;h4 id=&quot;功能实现&quot;&gt;功能实现&lt;/h4&gt;
&lt;p&gt;先预览一下页面最终的实现效果：&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.loli.net/2019/03/04/5c7cea40495b1.png&quot; alt=&quot;search-box&quot; /&gt;&lt;/p&gt;

&lt;p&gt;或者在这里：&lt;a href=&quot;https://knightyun.github.io/&quot;&gt;https://knightyun.github.io/&lt;/a&gt; 预览我的博客的搜索模块的最终实现；&lt;/p&gt;

&lt;h5 id=&quot;html部分&quot;&gt;HTML部分&lt;/h5&gt;
&lt;p&gt;即页面样式，组成很简单，即一个文本输入框&lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;input&amp;gt;&lt;/code&gt;和一个搜索图标，这里图标可以自行搜索下载一个，或者像下面一样使用在线图标，全部代码如下：&lt;/p&gt;

&lt;p&gt;先在&lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;header&amp;gt;&amp;lt;/header&amp;gt;&lt;/code&gt;内部添加以下代码，使用在线图标：&lt;/p&gt;
&lt;div class=&quot;language-html highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;link&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;href=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;https://fonts.googleapis.com/icon?family=Material+Icons&quot;&lt;/span&gt;
      &lt;span class=&quot;na&quot;&gt;rel=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;stylesheet&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后在网页内需要添加搜索栏的合适位置添加以下代码，一般放在顶部导航栏：&lt;/p&gt;
&lt;div class=&quot;language-html highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;div&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;class=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;search&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;i&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;class=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;material-icons search-icon search-start&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;search&lt;span class=&quot;nt&quot;&gt;&amp;lt;/i&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;input&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;type=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;text&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;class=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;search-input&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;placeholder=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Searching...&quot;&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;i&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;class=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;material-icons search-icon search-clear&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;clear&lt;span class=&quot;nt&quot;&gt;&amp;lt;/i&amp;gt;&lt;/span&gt;
	&lt;span class=&quot;nt&quot;&gt;&amp;lt;div&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;class=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;search-results&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;上面的&lt;strong&gt;clear&lt;/strong&gt;是一个清除输入框内容的图标，&lt;strong&gt;search-results&lt;/strong&gt;是用于输出匹配到的结果的板块；&lt;/p&gt;

&lt;h5 id=&quot;css部分&quot;&gt;CSS部分&lt;/h5&gt;
&lt;p&gt;然后来看一下&lt;strong&gt;CSS&lt;/strong&gt;样式代码，仅供参考：&lt;/p&gt;
&lt;div class=&quot;language-css highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nc&quot;&gt;.search&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;position&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;relative&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;height&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;30px&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;text-align&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;right&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;line-height&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;30px&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;padding-right&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;10px&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;nc&quot;&gt;.search&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;.search-icon&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;right&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;height&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;100%&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;margin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;10px&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;line-height&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;30px&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;cursor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;pointer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;py&quot;&gt;user-select&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;none&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;nc&quot;&gt;.search&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;.search-input&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;right&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;width&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;30%&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;height&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;30px&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;line-height&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;30px&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;margin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;border&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;2px&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;solid&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;#ddd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;border-radius&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;10px&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;box-sizing&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;border-box&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;nc&quot;&gt;.search&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;.search-clear&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;display&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;none&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;nc&quot;&gt;.search&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;.search-results&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;display&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;block&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;z-index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;position&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;absolute&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;top&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;30px&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;right&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;50px&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;width&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;50%&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;max-height&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;400px&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;overflow&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;auto&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;text-align&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;left&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;border-radius&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;5px&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;background&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;#ccc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;box-shadow&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;.3rem&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;.5rem&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;#333&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;nc&quot;&gt;.search&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;.search-results&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;.result-item&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;background&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;aqua&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;#000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;margin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;5px&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;padding&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;3px&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;border-radius&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;3px&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;cursor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;pointer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;样式可以自行随意调整，最终感觉好看就OK；&lt;/p&gt;

&lt;h5 id=&quot;javascript部分&quot;&gt;JavaScript部分&lt;/h5&gt;
&lt;p&gt;接下来就是重头戏了，也是实现搜索功能的核心部分，搜索逻辑的实现；&lt;/p&gt;

&lt;p&gt;再来大致分析一下逻辑，和实现的思路：&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;利用XMLHttpRequest()获取站内feed.xml内的所有数据，保存到一个XML DOM对象中；&lt;/li&gt;
  &lt;li&gt;将XML对象中的文章标题、链接、内容、索引等通过&lt;code class=&quot;highlighter-rouge&quot;&gt;getElementsByTagName()&lt;/code&gt;等方法获取并保存到对应数组变量中；&lt;/li&gt;
  &lt;li&gt;用户在输入框输入查找内容，提交后内容保存到一个字符串类型变量中；&lt;/li&gt;
  &lt;li&gt;遍历保存文章内容的数组，通过&lt;code class=&quot;highlighter-rouge&quot;&gt;.search()&lt;/code&gt;等方法和输入值进行匹配；&lt;/li&gt;
  &lt;li&gt;匹配成功后得到所有匹配成功的数组元素的索引值，该索引值也是该内容的标题、链接数组对应的索引值；&lt;/li&gt;
  &lt;li&gt;将最终搜集的文章标题、链接，以及匹配到的内容片段摘取输出到页面；&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;这里附上最终的 js 实现代码与注释：&lt;/p&gt;
&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// 获取搜索框、搜索按钮、清空搜索、结果输出对应的元素&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;searchBtn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;document&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;querySelector&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&#39;.search-start&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;searchClear&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;document&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;querySelector&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&#39;.search-clear&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;searchInput&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;document&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;querySelector&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&#39;.search-input&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;searchResults&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;document&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;querySelector&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&#39;.search-results&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// 申明保存文章的标题、链接、内容的数组变量&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;searchValue&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&#39;&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;arrItems&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[],&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;arrContents&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[],&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;arrLinks&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[],&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;arrTitles&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[],&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;arrResults&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[],&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;indexItem&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[];&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;tmpDiv&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;document&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;createElement&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&#39;div&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;tmpDiv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;className&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&#39;result-item&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// ajax 的兼容写法&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;xhr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;XMLHttpRequest&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;ActiveXObject&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&#39;Microsoft.XMLHTTP&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;xhr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;onreadystatechange&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;xhr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;readyState&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;xhr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;status&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;200&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nx&quot;&gt;xml&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;xhr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;responseXML&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;nx&quot;&gt;arrItems&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;xml&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;getElementsByTagName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&#39;item&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        
        &lt;span class=&quot;c1&quot;&gt;// 遍历并保存所有文章对应的标题、链接、内容到对应的数组中&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;arrItems&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;nx&quot;&gt;arrContents&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;arrItems&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;getElementsByTagName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&#39;description&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;childNodes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;nodeValue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;nx&quot;&gt;arrLinks&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;arrItems&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;getElementsByTagName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&#39;link&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;childNodes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;nodeValue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;replace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sr&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\s&lt;/span&gt;&lt;span class=&quot;sr&quot;&gt;+/g&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&#39;&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
            &lt;span class=&quot;nx&quot;&gt;arrTitles&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;arrItems&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;getElementsByTagName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&#39;title&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;childNodes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;nodeValue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// 开始获取根目录下 feed.xml 文件内的数据&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;xhr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;open&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&#39;get&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&#39;/feed.xml&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;xhr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;send&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;

&lt;span class=&quot;nx&quot;&gt;searchBtn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;onclick&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;searchConfirm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// 清空按钮点击函数&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;searchClear&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;onclick&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(){&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;searchInput&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;value&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&#39;&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;searchResults&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;style&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;display&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&#39;none&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;searchClear&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;style&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;display&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&#39;none&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// 输入框内容变化后就开始匹配，可以不用点按钮&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;searchInput&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;onkeydown&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;setTimeout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;searchConfirm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;searchInput&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;onfocus&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;searchResults&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;style&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;display&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&#39;block&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;searchConfirm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;searchInput&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;value&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&#39;&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nx&quot;&gt;searchResults&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;style&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;display&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&#39;none&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;nx&quot;&gt;searchClear&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;style&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;display&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&#39;none&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;searchInput&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;search&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sr&quot;&gt;/^&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\s&lt;/span&gt;&lt;span class=&quot;sr&quot;&gt;+$/&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    
        &lt;span class=&quot;c1&quot;&gt;// 检测输入值全是空白的情况&lt;/span&gt;
        &lt;span class=&quot;nx&quot;&gt;searchInit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
        &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;itemDiv&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;tmpDiv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;cloneNode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;nx&quot;&gt;itemDiv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;innerText&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&#39;请输入有效内容...&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;nx&quot;&gt;searchResults&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;appendChild&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;itemDiv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    
        &lt;span class=&quot;c1&quot;&gt;// 合法输入值的情况&lt;/span&gt;
        &lt;span class=&quot;nx&quot;&gt;searchInit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
        &lt;span class=&quot;nx&quot;&gt;searchValue&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;searchInput&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;nx&quot;&gt;searchMatching&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;arrContents&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;searchValue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// 每次搜索完成后的初始化&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;searchInit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;arrResults&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[];&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;indexItem&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[];&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;searchResults&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;innerHTML&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&#39;&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;searchResults&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;style&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;display&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&#39;block&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;searchClear&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;style&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;display&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&#39;block&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;searchMatching&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;arr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;// 在所有文章内容中匹配查询值&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;arr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;arr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;search&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;nx&quot;&gt;indexItem&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;push&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
            &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;indexContent&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;arr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;search&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
            &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;l&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;step&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            
            &lt;span class=&quot;c1&quot;&gt;// 将匹配到内容的地方进行黄色标记，并包括周围一定数量的文本&lt;/span&gt;
            &lt;span class=&quot;nx&quot;&gt;arrResults&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;push&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;arr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;slice&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;indexContent&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;step&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;indexContent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;
                &lt;span class=&quot;s1&quot;&gt;&#39;&amp;lt;mark&amp;gt;&#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;arr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;slice&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;indexContent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;indexContent&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&#39;&amp;lt;/mark&amp;gt;&#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;
                &lt;span class=&quot;nx&quot;&gt;arr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;slice&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;indexContent&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;indexContent&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;l&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;step&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;// 输出总共匹配到的数目&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;totalDiv&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;tmpDiv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;cloneNode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;totalDiv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;innerHTML&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&#39;总匹配：&amp;lt;b&amp;gt;&#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;indexItem&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;length&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&#39;&amp;lt;/b&amp;gt; 项&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;searchResults&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;appendChild&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;totalDiv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;// 未匹配到内容的情况&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;indexItem&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;length&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;itemDiv&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;tmpDiv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;cloneNode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;nx&quot;&gt;itemDiv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;innerText&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&#39;未匹配到内容...&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;nx&quot;&gt;searchResults&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;appendChild&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;itemDiv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;// 将所有匹配内容进行组合&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;arrResults&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;itemDiv&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;tmpDiv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;cloneNode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;nx&quot;&gt;itemDiv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;innerHTML&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&#39;&amp;lt;b&amp;gt;《&#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;arrTitles&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;indexItem&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;
            &lt;span class=&quot;s1&quot;&gt;&#39;》&amp;lt;/b&amp;gt;&amp;lt;hr /&amp;gt;&#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;arrResults&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
        &lt;span class=&quot;nx&quot;&gt;itemDiv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;setAttribute&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&#39;onclick&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&#39;changeHref(arrLinks[indexItem[&#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&#39;]])&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;nx&quot;&gt;searchResults&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;appendChild&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;itemDiv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;changeHref&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;href&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;// 在当前页面点开链接的情况&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;location&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;href&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;href&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;// 在新标签页面打开链接的代码，与上面二者只能取一个，自行决定&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// window.open(href);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;可以把上面的代码保存到&lt;code class=&quot;highlighter-rouge&quot;&gt;search-box.js&lt;/code&gt;这样的 js 文件中，然后引入到 html 页面里；&lt;/p&gt;

&lt;p&gt;看一下最终效果：
&lt;img src=&quot;https://i.loli.net/2019/03/04/5c7d2d2c18d93.png&quot; alt=&quot;final&quot; /&gt;&lt;/p&gt;

&lt;p&gt;或者来我的主页查看：&lt;strong&gt;&lt;a href=&quot;https://knightyun.github.io/&quot;&gt;https://knightyun.github.io/&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

            </description>
            <pubDate>Mon, 04 Mar 2019 00:00:00 +0000</pubDate>
            <guid>
                https://knightyun.github.io/2019/03/04/articles-search
            </guid>
        </item>
        
        <item>
            <title>编程范式之命令式与函数式</title>
            <link>
                https://knightyun.github.io/2019/01/27/programming-paradigm
            </link>
            <description>
                &lt;p&gt;很多语言是&lt;strong&gt;聚范式/多重范式&lt;/strong&gt;编程，即支持多在编程范式，如面向对象(Java)，面向过程(C语言)，泛函(函数式)，元程序设计等；以下例子都用 &lt;strong&gt;JavaScript&lt;/strong&gt; 举例；&lt;/p&gt;

&lt;h4 id=&quot;命令式编程imperative&quot;&gt;命令式编程(Imperative)&lt;/h4&gt;
&lt;p&gt;如命令一般指导程序一步步完成功能，如 for 循环：&lt;/p&gt;
&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;myFn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nx&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;myFn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;// 3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;函数式编程声明式functionaldeclarative&quot;&gt;函数式编程/声明式(Functional/Declarative)&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;函数式编程特点：&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;函数式编程是声明式的；&lt;/li&gt;
  &lt;li&gt;提倡纯函数理念，变量私有，不同于面向对象编程的成员共享；&lt;/li&gt;
  &lt;li&gt;无副作用，不影响其他外部变量；&lt;/li&gt;
  &lt;li&gt;有些类似初中数学纯函数f(x)的定义，提供输入值，返回新的输出值，每次提供相同输入值总能返回相同输出值，使线程安全可靠；&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;Array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;// 纯函数，输出唯一&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;Math&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;// 非纯函数，输出不唯一&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;函数的大量使用，变量中存储函数，动态创建函数，返回值为函数，函数作为参数传递，等等；&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// 字符串通过函数储存在变量中&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;myStr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(){&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&#39;Hello World&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;myStr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;// Hello World&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// 对象属性值储存为函数&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;myObj&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&#39;Cloud&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;getName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(){&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;myObj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;getName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// Cloud&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// 动态创建函数，用时调用，用完销毁&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&#39;Hello &#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(){&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&#39;World&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;})());&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;// Hello World&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// 函数作为参数进行传递&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;paraFn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&#39;Hello&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;myFn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;myFn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;paraFn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&#39;World&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// Hello World&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// 函数作为返回值&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;myFn2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&#39;Hello &#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(){&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&#39;World&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;myFn2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()());&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;// Hello World&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;总结：&lt;/strong&gt;例如for循环一个数组，命令式便是写出具体循环的方式，声明式便是只写声明函数，只要循环结果，具体方式交给程序执行；&lt;/p&gt;

&lt;p&gt;例如：&lt;/p&gt;
&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// 命令式&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[];&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;push&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;// [1, 4, 9]&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// 声明式&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;){&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;// [1, 4, 9]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;同样的结果，&lt;strong&gt;代码量和理解难易&lt;/strong&gt;上，声明式都明显优于命令式对吧；&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;声明式编程：&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;特点：&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;说明想要实现的功能，让机器完成步骤以及如何实现；&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;免去一些不必要的命令步骤，让思维集中在功能开发上，而不是冗长的复杂过程实现；&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;递归实现阶乘便是一个典型的函数式：&lt;/p&gt;
&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;factorial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;factorial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;factorial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;// 3 x 2 x 1 = 6&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;.map() .reduce()&lt;/strong&gt;等 也是申明式编程函数；&lt;/p&gt;

&lt;h4 id=&quot;函数合成&quot;&gt;函数合成&lt;/h4&gt;
&lt;p&gt;一个值变成另一个值，中间经过多个函数，将多个函数合并为一个函数来实现；&lt;/p&gt;

&lt;p&gt;举个例子：&lt;/p&gt;
&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;// 高中数学常见的过程
g(x) = 2x;
h(x) = x + 3;
f(x) = 2x + 3;
// 则可变换为以下形式，即我们所学的复合函数
f(x) = h(g(x));
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;上面的&lt;strong&gt;f(x)&lt;/strong&gt;便是一个合成函数，实现了变量&lt;strong&gt;x&lt;/strong&gt;到&lt;strong&gt;2x + 3&lt;/strong&gt;的转变；&lt;/p&gt;

&lt;p&gt;js的实现：&lt;/p&gt;
&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;gFn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;hFn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;hFn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;gFn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)));&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;// 5&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// 使用函数合成&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;fFn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;hFn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;gFn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;fFn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;// 5&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;函数柯理化currying&quot;&gt;函数柯理化(Currying)&lt;/h4&gt;
&lt;p&gt;以逻辑学家&lt;strong&gt;&lt;em&gt;Haskell Curry&lt;/em&gt;&lt;/strong&gt;命名，即使接收多个参数的函数变成接受单个参数的函数的过程。单参数会使函数合成更简单；&lt;/p&gt;

&lt;p&gt;例如：&lt;/p&gt;
&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// 原函数&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;plusFn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;z&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;z&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;plusFn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;// 6&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// 柯理化后&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;plusFn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;z&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;z&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;plusFn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// 6&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

            </description>
            <pubDate>Sun, 27 Jan 2019 00:00:00 +0000</pubDate>
            <guid>
                https://knightyun.github.io/2019/01/27/programming-paradigm
            </guid>
        </item>
        
        <item>
            <title>Kali Linux 自定义分辨率</title>
            <link>
                https://knightyun.github.io/2019/01/27/linux-xrandr
            </link>
            <description>
                &lt;p&gt;Kali中无分辨率1920 x 1080，自定义的步骤：&lt;/p&gt;

&lt;h4 id=&quot;方法一&quot;&gt;方法一&lt;/h4&gt;

&lt;p&gt;控制台输入：&lt;/p&gt;
&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;cvt 1920 1080
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;会得到以下内容：&lt;/p&gt;
&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# 1920x1080 59.96 Hz (CVT 2.07M9) hsync: 67.16 kHz; pclk: 173.00 MHz
Modeline &quot;1920x1080_60.00&quot;  173.00  1920 2048 2248 2576  1080 1083 1088 1120 -hsync +vsync
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;复制后面的内容，然后在新控制台输入：&lt;/p&gt;
&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;sudo xrandr --newmode &quot;1920x1080_60.00&quot; 173.00 1920 2048 2248 2576 1080 1083 1088 1120 -hsync +vsync
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后再输入：&lt;/p&gt;
&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;xrandr &lt;span class=&quot;nt&quot;&gt;--addmode&lt;/span&gt; Virtual1 &lt;span class=&quot;s2&quot;&gt;&quot;1920x1080_60.00&quot;&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;最后系统设置选择分辨率或者：&lt;/p&gt;
&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;xrandr &lt;span class=&quot;nt&quot;&gt;--output&lt;/span&gt; Virtual1 &lt;span class=&quot;nt&quot;&gt;--mode&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;1920x1080_60.00&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;但是此方法&lt;strong&gt;重启会失效&lt;/strong&gt;，因此可以使用方法二；&lt;/p&gt;

&lt;h4 id=&quot;方法二&quot;&gt;方法二&lt;/h4&gt;

&lt;p&gt;在 &lt;code class=&quot;highlighter-rouge&quot;&gt;/etc/profile&lt;/code&gt; 文件末尾假如以下代码：&lt;/p&gt;
&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;xrandr &lt;span class=&quot;nt&quot;&gt;--newmode&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;1920x1080_60.00&quot;&lt;/span&gt; 173.00 1920 2048 2248 2576 1080 1083 1088 1120 &lt;span class=&quot;nt&quot;&gt;-hsync&lt;/span&gt; +vsync
xrandr &lt;span class=&quot;nt&quot;&gt;--addmode&lt;/span&gt; Virtual1 &lt;span class=&quot;s2&quot;&gt;&quot;1920x1080_60.00&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;重启完成。&lt;/p&gt;

            </description>
            <pubDate>Sun, 27 Jan 2019 00:00:00 +0000</pubDate>
            <guid>
                https://knightyun.github.io/2019/01/27/linux-xrandr
            </guid>
        </item>
        
        <item>
            <title>Linux强行取消挂载</title>
            <link>
                https://knightyun.github.io/2019/01/27/linux-umount
            </link>
            <description>
                &lt;p&gt;Linux系统有时需要取消挂载一些设备或者目录，&lt;/p&gt;

&lt;p&gt;例如：&lt;/p&gt;
&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# /dev/sdb挂载到了 /mnt/usb&lt;/span&gt;
umount /mnt/usb
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;但是多半会提示：&lt;/p&gt;
&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;umount: /mnt/usb: target is busy
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;如果已&lt;strong&gt;备份&lt;/strong&gt;了数据需要强行卸载，并且尝试 &lt;code class=&quot;highlighter-rouge&quot;&gt;umount -f /mnt/usb&lt;/code&gt; 还是失败的情况&lt;/p&gt;

&lt;p&gt;可以使用命令：&lt;/p&gt;
&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;fuser &lt;span class=&quot;nt&quot;&gt;-cu&lt;/span&gt; /mnt/usb  &lt;span class=&quot;c&quot;&gt;#查看挂载文件进程&lt;/span&gt;
fuser &lt;span class=&quot;nt&quot;&gt;-mv&lt;/span&gt; /dev/sdb  &lt;span class=&quot;c&quot;&gt;#或者查看挂载点进程&lt;/span&gt;
fuser &lt;span class=&quot;nt&quot;&gt;-ck&lt;/span&gt; /mnt/usb  &lt;span class=&quot;c&quot;&gt;#结束进程&lt;/span&gt;
fuser &lt;span class=&quot;nt&quot;&gt;-mk&lt;/span&gt; /dev/sdb  &lt;span class=&quot;c&quot;&gt;#使用挂载点结束进程&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;或者使用“懒卸载”方式，命令执行后系统会自动关闭相关进程后再卸载：&lt;/p&gt;
&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;umount &lt;span class=&quot;nt&quot;&gt;-l&lt;/span&gt; /mnt/usb
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

            </description>
            <pubDate>Sun, 27 Jan 2019 00:00:00 +0000</pubDate>
            <guid>
                https://knightyun.github.io/2019/01/27/linux-umount
            </guid>
        </item>
        
        <item>
            <title>Linux中apt install的lock问题</title>
            <link>
                https://knightyun.github.io/2019/01/27/linux-apt-lock
            </link>
            <description>
                &lt;p&gt;Linux系统中有时执行 apt install 时，可能会显示以下问题：&lt;/p&gt;
&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;E: 无法获得锁 /var/lib/dpkg/lock - open (11: 资源暂时不可用)
E: 无法锁定管理目录(/var/lib/dpkg/)，是否有其他进程正占用它？
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;根据提示分别找到对应的两个lock文件，移除即可；&lt;/p&gt;

&lt;p&gt;解决方法：&lt;/p&gt;
&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;rm /var/cache/apt/archives/lock
&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;rm /var/lib/dpkg/lock
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;再次运行就成功了。&lt;/p&gt;

            </description>
            <pubDate>Sun, 27 Jan 2019 00:00:00 +0000</pubDate>
            <guid>
                https://knightyun.github.io/2019/01/27/linux-apt-lock
            </guid>
        </item>
        
        <item>
            <title>JavaScript深度迭代遍历未知对象</title>
            <link>
                https://knightyun.github.io/2019/01/27/js-iteration
            </link>
            <description>
                &lt;p&gt;面向对象编程的语言，都存在对对象的一些操作，其中就包括遍历未知对象的属性值。&lt;/p&gt;

&lt;h4 id=&quot;通常情况&quot;&gt;通常情况&lt;/h4&gt;

&lt;p&gt;常见的遍历对象的方法：&lt;/p&gt;
&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;o&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&#39;cloud&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;age&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&#39;: &#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// name: cloud&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// age: 20&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;特殊情况&quot;&gt;特殊情况&lt;/h4&gt;

&lt;p&gt;但是对象中又含有子对象，对象的属性又是另一个对象，或者更深层嵌套，上面方法就不适用了；&lt;/p&gt;

&lt;p&gt;下面使用&lt;strong&gt;递归&lt;/strong&gt;实现这个功能：&lt;/p&gt;
&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;o&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;firstName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&#39;cloud&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;lastName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&#39;huang&#39;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;age&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;myFn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nx&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&#39;: &#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]);&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;// 这里使用递归，属性类型为对象则进一步遍历&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;typeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&#39;object&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;nx&quot;&gt;myFn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]);&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;myFn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// 输出：&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// name: [object Object]&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// firstName: cloud&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// lastName: huang&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// age: 20&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;这样的话不论对象有多复杂的结构都能全部遍历到位；&lt;/p&gt;

&lt;h4 id=&quot;困境&quot;&gt;困境&lt;/h4&gt;

&lt;p&gt;但同时，这也是个问题，一些对象层次非常深甚至是死循环的情况就尴尬了，类似于子对象属性与父对象属性一样，尝试用上诉函数遍历一下浏览器的&lt;code class=&quot;highlighter-rouge&quot;&gt;window&lt;/code&gt; 对象就能体会了，你会后悔的；&lt;/p&gt;

&lt;p&gt;所以为避免这种尴尬情况，设置一个&lt;strong&gt;迭代深度值&lt;/strong&gt;吧，指定遍历到第几代：&lt;/p&gt;
&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;depth&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;// depth为迭代深度值&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;myFn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nx&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&#39;: &#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]);&lt;/span&gt;
        &lt;span class=&quot;nx&quot;&gt;depth&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;depth&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;typeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&#39;object&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;nx&quot;&gt;myFn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]);&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;或者使用一种类似&lt;strong&gt;懒加载&lt;/strong&gt;的形式：&lt;/p&gt;
&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;myFn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nx&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&#39;: &#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]);&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;typeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&#39;object&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;c1&quot;&gt;// 判断用户是否要继续迭代&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;confirmFn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&#39;是否深入遍历？&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;nx&quot;&gt;myFn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]);&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

            </description>
            <pubDate>Sun, 27 Jan 2019 00:00:00 +0000</pubDate>
            <guid>
                https://knightyun.github.io/2019/01/27/js-iteration
            </guid>
        </item>
        
        <item>
            <title>css属性前浏览器厂商前缀</title>
            <link>
                https://knightyun.github.io/2019/01/27/css-webkit
            </link>
            <description>
                &lt;p&gt;CSS3中一些新功能也是目前导致各大浏览器不兼容的一个原因，这些新功能的出现，浏览器厂商们变便开始尝试融合、试验，所以就在这些功能前加上自己的特定前缀来执行自己的特定解决方法，为了让这些功能能在完全确认下来前使用；&lt;/p&gt;

&lt;p&gt;下面就是我们经常用到的前缀及其兼容浏览器：&lt;/p&gt;

&lt;h4 id=&quot;-webkit-&quot;&gt;-webkit-&lt;/h4&gt;
&lt;p&gt;Apple Webkit团队，兼容Android, Safari, Chrome, BlackBerry等；&lt;/p&gt;

&lt;h4 id=&quot;-moz-&quot;&gt;-moz-&lt;/h4&gt;
&lt;p&gt;Mozilla，兼容Firefox等;&lt;/p&gt;

&lt;h4 id=&quot;-ms-&quot;&gt;-ms-&lt;/h4&gt;
&lt;p&gt;Microsoft基金会，兼容IE;&lt;/p&gt;

&lt;h4 id=&quot;-o-&quot;&gt;-o-&lt;/h4&gt;
&lt;p&gt;兼容Opera, Opera Mini, Opera Mobile;&lt;/p&gt;

&lt;p&gt;因此对于一些较新的css3特性，需要添加以上前缀兼容每个浏览器，例如实现线性渐变，标准写法是 &lt;code class=&quot;highlighter-rouge&quot;&gt;linear-gradient()&lt;/code&gt;，但是一下浏览器还未完全确定这一特性，就在前面添加一个前缀来进行试验执行，如 &lt;code class=&quot;highlighter-rouge&quot;&gt;-webkit-linear-gradient&lt;/code&gt;；&lt;/p&gt;

&lt;p&gt;下面是开发中常用的兼容写法：&lt;/p&gt;
&lt;div class=&quot;language-css highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;body&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;background&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;linear-gradient&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;green&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;blue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;background&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;-webkit-linear-gradient&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;green&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;blue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;background&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;-moz-linear-gradient&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;green&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;blue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;background&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;-o-linear-gradient&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;green&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;blue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;background&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;-ms-linear-gradient&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;green&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;blue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

            </description>
            <pubDate>Sun, 27 Jan 2019 00:00:00 +0000</pubDate>
            <guid>
                https://knightyun.github.io/2019/01/27/css-webkit
            </guid>
        </item>
        
        <item>
            <title>CSS3过渡与动画</title>
            <link>
                https://knightyun.github.io/2019/01/27/css-transition-animation
            </link>
            <description>
                &lt;p&gt;CSS3中出现很多新的特性，下面就讲一下其中比较好玩的&lt;strong&gt;3D操作&lt;/strong&gt;，&lt;strong&gt;过渡&lt;/strong&gt;和&lt;strong&gt;动画&lt;/strong&gt;效果；&lt;/p&gt;

&lt;h4 id=&quot;过渡transition&quot;&gt;过渡(transition)&lt;/h4&gt;
&lt;p&gt;过渡就是使瞬间的样式变化，按照一定方式变得缓慢平缓；&lt;/p&gt;

&lt;p&gt;例如鼠标划过超链接时颜色的变化，点击按钮后的颜色变化等，默认转化都是瞬间完成，可能你已经习惯了这种变换，但有时候平缓一些看着还是比较舒适的；&lt;/p&gt;

&lt;p&gt;要实现样式的过渡变化，那么首先就学要有&lt;strong&gt;样式变换&lt;/strong&gt;，例如鼠标划过，单击按钮，点击图片等操作，来实现颜色，尺寸，位置等样式的变化；&lt;/p&gt;

&lt;p&gt;下面是鼠标划过段落使文本变红的操作，应用所有 &lt;code class=&quot;highlighter-rouge&quot;&gt;transition&lt;/code&gt;属性：&lt;/p&gt;
&lt;div class=&quot;language-css highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;nd&quot;&gt;:hover&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;red&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;transition-property&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;transition-duration&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;2s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;transition-timing-function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;linear&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;transition-delay&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;img src=&quot;https://i.loli.net/2019/01/26/5c4c3488ab322.gif&quot; alt=&quot;image&quot; /&gt;&lt;/p&gt;

&lt;p&gt;一共四个值，功能基本都是字面翻译的意思：&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;transition-property&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;执行过渡的属性，例子设置为颜色color的变化，也可以是width, font-size等，不设置的话默认是&lt;code class=&quot;highlighter-rouge&quot;&gt;all&lt;/code&gt;，即所有属性；&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;transition-duration&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;过渡的时间，单位是秒，如1s, 2.3s，不设置的话默认 &lt;code class=&quot;highlighter-rouge&quot;&gt;0s&lt;/code&gt;，即无过渡效果；&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;transition-timing-function&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;设置过渡时的变化方式，默认是 &lt;code class=&quot;highlighter-rouge&quot;&gt;ease&lt;/code&gt;，即速度由慢到快再到慢，常用的还有 &lt;code class=&quot;highlighter-rouge&quot;&gt;linear&lt;/code&gt;，线性变化速度均匀，还有其他几个方式，过渡时间短的话看不出什么区别；&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;transition-delay&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;延迟时间，即多少秒后执行过渡效果，默认 &lt;code class=&quot;highlighter-rouge&quot;&gt;0s&lt;/code&gt;，不延迟；&lt;/p&gt;

&lt;p&gt;当然这么多单词可能记不住，一般使用快捷写法：&lt;/p&gt;
&lt;div class=&quot;language-css highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;transition&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;color&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;2s&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;linear&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;/*最少要指定过渡时间*/&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;transition&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;2s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;也可以设置每个样式分别过渡，例如：&lt;/p&gt;
&lt;div class=&quot;language-css highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;transition&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;color&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;2s&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;linear&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;font-size&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;.5s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;background&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;1s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;每个样式过渡之间用&lt;strong&gt;逗号&lt;/strong&gt;隔开就行了；&lt;/p&gt;

&lt;p&gt;最后，由于是新特性，为了兼容性需要加上浏览器厂商前缀：&lt;/p&gt;
&lt;div class=&quot;language-css highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;transition&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;2s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;-webkit-transition&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;2s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;-moz-transition&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;2s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;-ms-transition&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;2s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;-o-transition&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;2s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;动画animation&quot;&gt;动画(animation)&lt;/h4&gt;
&lt;p&gt;CSS3的动画是个很不错的技术，基本能取代一些动图，javascript，flash等；&lt;/p&gt;

&lt;p&gt;而动画里最重要的概念就是&lt;strong&gt;关键帧&lt;/strong&gt;，也许你用PS做gif动图的时候看见过这个概念，所谓动画就是一帧一帧图片连续切换实现的效果，关键帧就是里面主要的一些帧；&lt;/p&gt;

&lt;p&gt;实现CSS动画也需要设置关键帧 &lt;code class=&quot;highlighter-rouge&quot;&gt;@keyframes&lt;/code&gt;：&lt;/p&gt;
&lt;div class=&quot;language-css highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;@keyframes&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;my-animation&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nl&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;red&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nl&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;green&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nl&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;blue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;格式如上，@keyframes后面跟的是自定义的&lt;strong&gt;动画名称&lt;/strong&gt;，后面会用到。里面的&lt;strong&gt;0%，50%，100%&lt;/strong&gt;便是设置的三个关键帧及其对应样式，如果只需要设置首尾两个关键帧，可以这样写：&lt;/p&gt;
&lt;div class=&quot;language-css highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;@keyframes&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;my-animation&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nl&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;green&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;to&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nl&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;blue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;效果：&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.loli.net/2019/01/26/5c4c4eae8329a.gif&quot; alt=&quot;image&quot; /&gt;&lt;/p&gt;

&lt;p&gt;当然样式除了color还能设置多项样式；&lt;/p&gt;

&lt;p&gt;定义好关键帧后就直接在需要应用动画的元素标签内使用就行了，格式及所有属性如下：&lt;/p&gt;
&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;p {
    animation-name: my-animation;
    animation-duration: 3s;
    animation-timing-function: ease;
    animation-delay: 0;
    animation-iteration-count: 3;
    animation-direction: normal;
    animation-play-state: running;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;发现了吧，很多属性和&lt;strong&gt;transition&lt;/strong&gt;里面一样，简单介绍下：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;animation-name&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;就是之前跟在@keyframea后面的自定义名称，之前设置的是 &lt;code class=&quot;highlighter-rouge&quot;&gt;my-animation&lt;/code&gt;;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;animation-duration&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;animation-timing-function&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;animation-delay&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;和前面一样，默认分别为 &lt;code class=&quot;highlighter-rouge&quot;&gt;0, ease, 0&lt;/code&gt;；&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;animation-iteration-count&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;动画播放的次数，默认 &lt;code class=&quot;highlighter-rouge&quot;&gt;1&lt;/code&gt;，但一般设置为 &lt;code class=&quot;highlighter-rouge&quot;&gt;infinite&lt;/code&gt;，即无限循环；&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;animation-direction&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;动画播放的方向，&lt;code class=&quot;highlighter-rouge&quot;&gt;normal&lt;/code&gt;为默认，正向播放，&lt;code class=&quot;highlighter-rouge&quot;&gt;reverse&lt;/code&gt;为反向播放，&lt;code class=&quot;highlighter-rouge&quot;&gt;alternate&lt;/code&gt;为正向后反向，&lt;code class=&quot;highlighter-rouge&quot;&gt;alternate-reverse&lt;/code&gt;为反向后正向；&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;animation-play-state&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;播放状态，默认 &lt;code class=&quot;highlighter-rouge&quot;&gt;running&lt;/code&gt;，运行，&lt;code class=&quot;highlighter-rouge&quot;&gt;paused&lt;/code&gt;为暂停，可以在javascript中使用对动画进行控制；&lt;/p&gt;

&lt;p&gt;当然，这个属性比之前的transition还多，也有简便写法：&lt;/p&gt;
&lt;div class=&quot;language-css highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;animation&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;my-animation&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;3s&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;linear&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;infinite&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;alternate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;其中 &lt;code class=&quot;highlighter-rouge&quot;&gt;animation-name&lt;/code&gt; 和 &lt;code class=&quot;highlighter-rouge&quot;&gt;animation-duration&lt;/code&gt;为必须设置的属性；&lt;/p&gt;

&lt;p&gt;同样，记得考虑浏览器兼容：&lt;/p&gt;
&lt;div class=&quot;language-css highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;@-webkit-keyframes&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mycanimation&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nl&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;green&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;to&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nl&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;blue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;-webkit-animation&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;my-animation&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;3s&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;linear&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;infinite&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;/* -moz-, -ms-, -o- 格式类似 */&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

            </description>
            <pubDate>Sun, 27 Jan 2019 00:00:00 +0000</pubDate>
            <guid>
                https://knightyun.github.io/2019/01/27/css-transition-animation
            </guid>
        </item>
        
        <item>
            <title>CSS3之2D与3D变换</title>
            <link>
                https://knightyun.github.io/2019/01/27/css-transform
            </link>
            <description>
                &lt;h4 id=&quot;目录&quot;&gt;目录&lt;/h4&gt;

&lt;hr /&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#%E5%85%B3%E4%BA%8E%E5%9D%90%E6%A0%87%E8%BD%B4&quot;&gt;关于坐标轴&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#2d%E5%8F%98%E6%8D%A2&quot;&gt;2D变换&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#translatexy&quot;&gt;translate(x,y)&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#rotatedeg&quot;&gt;rotate(deg)&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#scalexy&quot;&gt;scale(x,y)&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#skewxdegydeg&quot;&gt;skew(xdeg,ydeg)&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#matrixabcde&quot;&gt;matrix(a,b,c,d,e)&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#3d%E5%8F%98%E6%8D%A2&quot;&gt;3D变换&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#translate3dxyz&quot;&gt;translate3d(x,y,z)&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#rotate3dxyzdeg&quot;&gt;rotate3d(x,y,z,deg)&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#scale3dxyz&quot;&gt;scale3d(x,y,z)&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#matrix3d&quot;&gt;matrix3d()&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#perspective&quot;&gt;perspective&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#perspective-origin&quot;&gt;perspective-origin&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#backface-visibility&quot;&gt;backface-visibility&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%E5%85%B6%E4%BB%96%E5%B1%9E%E6%80%A7&quot;&gt;其他属性&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#transform-origin&quot;&gt;transform-origin&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#transform-style&quot;&gt;transform-style&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;p&gt;css3实现了对元素执行2D平面变换，以及视觉上的3D空间变换，2D变换平时可能用的较多，3D效果也能锦上添花；&lt;/p&gt;

&lt;h4 id=&quot;关于坐标轴&quot;&gt;关于坐标轴&lt;/h4&gt;

&lt;p&gt;初中数学的几何学里我们便开始接触坐标轴，最基本的是平面直角坐标系&lt;code class=&quot;highlighter-rouge&quot;&gt;XoY&lt;/code&gt;，然后延伸出空间直角坐标系&lt;code class=&quot;highlighter-rouge&quot;&gt;XYZ&lt;/code&gt;，现在我们来说一下css中的坐标系；&lt;/p&gt;

&lt;p&gt;css甚至一下设备相关的开发中，基本都遵循这样一套坐标系：以手机屏幕为例，坐标系&lt;strong&gt;圆点&lt;/strong&gt;位于屏幕最左上角；&lt;strong&gt;x轴&lt;/strong&gt;水平，向右为正方向；&lt;strong&gt;y轴&lt;/strong&gt;垂直，向下为正方向；&lt;strong&gt;z轴&lt;/strong&gt;垂直于整个屏幕平面，向外为正方向，就是屏幕光线射向你眼睛的方向；&lt;/p&gt;

&lt;p&gt;如图：&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.loli.net/2019/01/26/5c4c05d7bfbf0.png&quot; alt=&quot;image&quot; /&gt;&lt;/p&gt;

&lt;h4 id=&quot;2d变换&quot;&gt;2D变换&lt;/h4&gt;

&lt;p&gt;包括平移&lt;code class=&quot;highlighter-rouge&quot;&gt;translate()&lt;/code&gt;，旋转&lt;code class=&quot;highlighter-rouge&quot;&gt;rotate()&lt;/code&gt;，缩放&lt;code class=&quot;highlighter-rouge&quot;&gt;scale()&lt;/code&gt;，倾斜&lt;code class=&quot;highlighter-rouge&quot;&gt;skew()&lt;/code&gt;，矩阵&lt;code class=&quot;highlighter-rouge&quot;&gt;matrix()&lt;/code&gt;；&lt;/p&gt;

&lt;h5 id=&quot;translatexy&quot;&gt;translate(x,y)&lt;/h5&gt;

&lt;p&gt;平移操作，包括&lt;code class=&quot;highlighter-rouge&quot;&gt;translateX(x)&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;translateY(y)&lt;/code&gt;，括号内填平移参数值，可以是负值，即反方向；&lt;/p&gt;

&lt;p&gt;例如：&lt;/p&gt;
&lt;div class=&quot;language-css highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;div&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;c&quot;&gt;/*元素向右平移50px，向下移60px*/&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;transform&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;translateX&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;50px&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;transform&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;translateY&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;60px&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;/*简写形式，效果相同*/&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;div&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;transform&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;translate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;50px&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;60px&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;注意&lt;/strong&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;translate()&lt;/code&gt;只指定一个值则默认是&lt;strong&gt;x轴&lt;/strong&gt;位移，即水平移动；&lt;/p&gt;

&lt;h5 id=&quot;rotatedeg&quot;&gt;rotate(deg)&lt;/h5&gt;

&lt;p&gt;元素旋转，括号中参数为旋转角度，&lt;strong&gt;顺时针&lt;/strong&gt;为正值，&lt;strong&gt;逆时针&lt;/strong&gt;为负值，单位为&lt;code class=&quot;highlighter-rouge&quot;&gt;deg&lt;/code&gt;，即多少度；&lt;/p&gt;

&lt;p&gt;例如：&lt;/p&gt;
&lt;div class=&quot;language-css highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;div&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;c&quot;&gt;/* 顺时针旋转30° */&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;transform&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rotate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;30deg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;img src=&quot;https://i.loli.net/2019/01/26/5c4c1133253ba.png&quot; alt=&quot;rotate&quot; /&gt;&lt;/p&gt;

&lt;h5 id=&quot;scalexy&quot;&gt;scale(x,y)&lt;/h5&gt;

&lt;p&gt;缩放元素，参数分别为x轴，y轴缩放倍数，包括&lt;code class=&quot;highlighter-rouge&quot;&gt;scaleX(x)&lt;/code&gt;和&lt;code class=&quot;highlighter-rouge&quot;&gt;scaleY(y)&lt;/code&gt;，提供一个参数表示&lt;strong&gt;按比例&lt;/strong&gt;缩放；&lt;/p&gt;

&lt;p&gt;例如：&lt;/p&gt;
&lt;div class=&quot;language-css highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;div&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;c&quot;&gt;/* 横向缩小一半，纵向放大一倍 */&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;transform&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;scale&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;.5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;img src=&quot;https://i.loli.net/2019/01/26/5c4c11e1729ad.png&quot; alt=&quot;scale1&quot; /&gt;&lt;/p&gt;

&lt;div class=&quot;language-css highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;div&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;c&quot;&gt;/* 按比例放大3倍 */&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;transform&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;scale&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;img src=&quot;https://i.loli.net/2019/01/26/5c4c11e1b4e89.png&quot; alt=&quot;scale2&quot; /&gt;&lt;/p&gt;

&lt;h5 id=&quot;skewxdegydeg&quot;&gt;skew(xdeg,ydeg)&lt;/h5&gt;

&lt;p&gt;包含&lt;code class=&quot;highlighter-rouge&quot;&gt;skewX(deg)&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;skewY(deg)&lt;/code&gt;，表示在水平和垂直方向倾斜的角度；&lt;/p&gt;

&lt;p&gt;例如：&lt;/p&gt;
&lt;div class=&quot;language-css highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;div&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;transform&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;skewX&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;30deg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;transform&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;skewY&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;45deg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;/* 简写 */&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;div&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;transform&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;skew&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;30deg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;45deg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;需要&lt;strong&gt;注意&lt;/strong&gt;，如果元素为一个矩形，那么&lt;code class=&quot;highlighter-rouge&quot;&gt;skewX(30deg)&lt;/code&gt;表示矩形&lt;strong&gt;顶边固定&lt;/strong&gt;，底边&lt;strong&gt;向右&lt;/strong&gt;倾斜&lt;code class=&quot;highlighter-rouge&quot;&gt;30deg&lt;/code&gt;；&lt;code class=&quot;highlighter-rouge&quot;&gt;skewY(30deg)&lt;/code&gt;表示矩形&lt;strong&gt;左边框固定&lt;/strong&gt;，右边框&lt;strong&gt;向下&lt;/strong&gt;倾斜&lt;code class=&quot;highlighter-rouge&quot;&gt;30deg&lt;/code&gt;；&lt;/p&gt;

&lt;p&gt;可以根据上面讲的屏幕坐标系来记忆，x轴位于屏幕顶部，方向向右；y轴位于屏幕左部，方向向下；&lt;/p&gt;

&lt;p&gt;如果&lt;code class=&quot;highlighter-rouge&quot;&gt;skew()&lt;/code&gt;只指定一个值，默认是&lt;strong&gt;水平倾斜&lt;/strong&gt;；&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;skewX(30deg)&lt;/strong&gt; 的效果：&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.loli.net/2019/01/26/5c4c0fc60c6bd.png&quot; alt=&quot;skewX&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;skewY(30deg)&lt;/strong&gt; 的效果：&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.loli.net/2019/01/26/5c4c10c4b187a.png&quot; alt=&quot;skewY&quot; /&gt;&lt;/p&gt;

&lt;h5 id=&quot;matrixabcde&quot;&gt;matrix(a,b,c,d,e)&lt;/h5&gt;

&lt;p&gt;这是一个综合属性，之前的平移，缩放，旋转，倾斜都能通过这个矩阵函数实现，对，大学里&lt;strong&gt;线性代数&lt;/strong&gt;中的矩阵 T_T；&lt;/p&gt;

&lt;p&gt;其实这个函数就是前面四种操作的&lt;strong&gt;原理&lt;/strong&gt;，函数共有六个参数，四种操作都对应不同的参数改变方式，像我们这种非数学专业的就不赘述原理了，前面的操作基本够用了(想寻找刺激就去百度“css matrix”吧)~~;&lt;/p&gt;

&lt;h4 id=&quot;3d变换&quot;&gt;3D变换&lt;/h4&gt;

&lt;p&gt;所谓3D就是在前面2D平面上多了一个&lt;strong&gt;z轴&lt;/strong&gt;，方法名也差不多，然后能以分别以三根轴位基准进行变换，实现立体效果；&lt;/p&gt;

&lt;p&gt;看一下所有3D操作方法：&lt;/p&gt;
&lt;h5 id=&quot;translate3dxyz&quot;&gt;translate3d(x,y,z)&lt;/h5&gt;

&lt;p&gt;结合前面讲的空间坐标系和 x, y, z轴的位置，三个参数分别对应元素在三个坐标轴方向的平移值，也包含三个方法&lt;code class=&quot;highlighter-rouge&quot;&gt;translateX(x)&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;translateY(y)&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;translateZ(z)&lt;/code&gt;；&lt;/p&gt;

&lt;p&gt;举例：&lt;/p&gt;
&lt;div class=&quot;language-css highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;div&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;transform&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;translateX&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;50px&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;transform&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;translateY&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;60px&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;transform&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;translateZ&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;70px&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;/* 简写 */&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;div&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;transform&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;translate3d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;50px&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;60px&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;70px&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;注意：&lt;/strong&gt; 关于设置 &lt;code class=&quot;highlighter-rouge&quot;&gt;translateZ(z)&lt;/code&gt; 看不出效果的问题，后面说到设置&lt;code class=&quot;highlighter-rouge&quot;&gt;persoective&lt;/code&gt;时会解释；&lt;/p&gt;

&lt;h5 id=&quot;rotate3dxyzdeg&quot;&gt;rotate3d(x,y,z,deg)&lt;/h5&gt;

&lt;p&gt;参数&lt;code class=&quot;highlighter-rouge&quot;&gt;x, y, z&lt;/code&gt;为空间坐标系的一个坐标位置，然后由原点&lt;code class=&quot;highlighter-rouge&quot;&gt;(0, 0, 0)&lt;/code&gt;指向这个点形成一个有方向的新轴，数学中称矢量，最后一个参数就是元素围绕刚才所形成的新轴旋转的度数；&lt;/p&gt;

&lt;p&gt;也包括 &lt;code class=&quot;highlighter-rouge&quot;&gt;rotateX(deg)&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;rotateY(deg)&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;rotateZ(deg)&lt;/code&gt;，之前2D的 &lt;code class=&quot;highlighter-rouge&quot;&gt;rotate()&lt;/code&gt; 便是这里的 &lt;code class=&quot;highlighter-rouge&quot;&gt;rotateZ()&lt;/code&gt;；&lt;/p&gt;

&lt;p&gt;至于旋转的方向，判断方法类似于物理中的&lt;strong&gt;左手定则&lt;/strong&gt;：角度指定为正的话，左手拇指与四指垂直，拇指指向元素围绕旋转的坐标轴或自定义轴，四指弯曲围绕方向就是旋转方向；&lt;/p&gt;

&lt;p&gt;举例：&lt;/p&gt;
&lt;div class=&quot;language-css highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;div&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;transform&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rotateX&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;30deg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;transform&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rotateY&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;30deg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;transform&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rotataZ&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;30deg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;/* 自定义轴旋转 */&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;div&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;transform&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rotate3d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;30deg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;rotateX(30deg)&lt;/strong&gt; 的效果：&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.loli.net/2019/01/26/5c4c13d0c4eef.png&quot; alt=&quot;rotateX&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;rotateY(30deg)&lt;/strong&gt; 的效果：&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.loli.net/2019/01/26/5c4c142489a1d.png&quot; alt=&quot;rotateY&quot; /&gt;&lt;/p&gt;

&lt;p&gt;关于为什么这里的旋转不是想象中的效果，而是缩小，主要是没有设置视点，后面会讲；&lt;/p&gt;

&lt;h5 id=&quot;scale3dxyz&quot;&gt;scale3d(x,y,z)&lt;/h5&gt;

&lt;p&gt;元素关于三个轴的缩放比例，包括&lt;code class=&quot;highlighter-rouge&quot;&gt;scaleX(x)&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;scaleY(y)&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;scaleZ(z)&lt;/code&gt;，举例：&lt;/p&gt;

&lt;div class=&quot;language-css highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;div&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;transform&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;scaleX&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;transform&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;scaleY&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;transform&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;scaleZ&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;/* 简写 */&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;div&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;transform&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;scale3d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;需要&lt;strong&gt;注意&lt;/strong&gt;这里的&lt;code class=&quot;highlighter-rouge&quot;&gt;scaleZ()&lt;/code&gt;，正常情况下，扩大z轴会是物体&lt;strong&gt;变厚&lt;/strong&gt;，但是css里面呈现的平面元素并没有&lt;strong&gt;厚度&lt;/strong&gt;，所以这里的缩放z轴其实是缩放元素在z轴的&lt;strong&gt;坐标&lt;/strong&gt;，所以要有效果必须要指定&lt;code class=&quot;highlighter-rouge&quot;&gt;translateZ()&lt;/code&gt;的值；&lt;/p&gt;

&lt;p&gt;举例：&lt;/p&gt;
&lt;div class=&quot;language-css highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;body&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;perspective&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;500&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;div&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;c&quot;&gt;/* 必须这个顺序，先缩放后平移，不然无效果 */&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;transform&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;scaleZ&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;translateZ&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;100px&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;img src=&quot;https://i.loli.net/2019/01/26/5c4c17ef48ae2.png&quot; alt=&quot;scaleZ&quot; /&gt;&lt;/p&gt;

&lt;p&gt;按照上面样式才能看到&lt;code class=&quot;highlighter-rouge&quot;&gt;scaleZ(2)&lt;/code&gt;的效果，因为后面在z轴上移动了&lt;code class=&quot;highlighter-rouge&quot;&gt;100px&lt;/code&gt;，缩放比例为2，最终会移动&lt;code class=&quot;highlighter-rouge&quot;&gt;200px&lt;/code&gt;，屏幕上则表现为元素放大了一下，这是透视效果，就是那个 &lt;code class=&quot;highlighter-rouge&quot;&gt;perspective&lt;/code&gt; 值，下面会讲到；&lt;/p&gt;

&lt;h5 id=&quot;matrix3d&quot;&gt;matrix3d()&lt;/h5&gt;

&lt;p&gt;和前面2D的&lt;code class=&quot;highlighter-rouge&quot;&gt;matrix()&lt;/code&gt;相似，只不过这里括号里的参数有&lt;strong&gt;16个&lt;/strong&gt;，矩阵更加复杂，跳过吧﹋o﹋，有兴趣可以自行百度~~；&lt;/p&gt;

&lt;h5 id=&quot;perspective&quot;&gt;perspective&lt;/h5&gt;

&lt;p&gt;在上面的示例中，有关z轴的平移和缩放通常情况下是看不出效果的，正是缺少这项属性值，叫做 &lt;strong&gt;透视&lt;/strong&gt;，美术或设计中会出现这个词汇，就是实现物体近大远小的效果，远小最终会小到一个点，那就是 &lt;strong&gt;透视点&lt;/strong&gt;，&lt;code class=&quot;highlighter-rouge&quot;&gt;perspective&lt;/code&gt;就是用来设置那个点距离元素有多远，一般300~600很体现很好的透视效果，&lt;strong&gt;值越小元素透视变形越严重&lt;/strong&gt;；&lt;/p&gt;

&lt;p&gt;需要&lt;strong&gt;注意&lt;/strong&gt;的是，这项属性设置在应用透视效果元素的&lt;strong&gt;父元素&lt;/strong&gt;的样式中，才能看出效果，例如：&lt;/p&gt;
&lt;div class=&quot;language-css highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;body&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;perspective&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;500&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;c&quot;&gt;/* 考虑浏览器兼容 */&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;-webkit-perspective&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;500&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;也可以设置在元素本身，格式为：&lt;/p&gt;
&lt;div class=&quot;language-css highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;div&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;transform&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;perspective&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;500&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;-webkit-transform&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;perspective&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;500&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;rotateX(45deg)&lt;/strong&gt; 的更真实的效果：&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.loli.net/2019/01/26/5c4c1929babb1.png&quot; alt=&quot;rotateX&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;rotateY(45deg)&lt;/strong&gt; 的效果：&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.loli.net/2019/01/26/5c4c19c45bbb4.png&quot; alt=&quot;rotateY&quot; /&gt;&lt;/p&gt;

&lt;h5 id=&quot;perspective-origin&quot;&gt;perspective-origin&lt;/h5&gt;

&lt;p&gt;此项设置透视点的位置，默认在元素几何中心，需要设置的话，格式如下：&lt;/p&gt;
&lt;div class=&quot;language-css highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;body&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;c&quot;&gt;/* 默认中心 */&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;perspective-origin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;center&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;center&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;c&quot;&gt;/* 左上角 */&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;perspective-origin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;left&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;top&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;c&quot;&gt;/* 右边中心 */&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;perspective-origin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;right&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;center&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;c&quot;&gt;/* 底部中心 */&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;perspective-origin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;bottom&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;center&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;c&quot;&gt;/* 也可以是长度 */&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;perspective-origin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;30px&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;40px&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;c&quot;&gt;/*最后记得加 -webkit- 兼容 */&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;perspective-origin: left center&lt;/strong&gt; 的效果：&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.loli.net/2019/01/26/5c4c1c4ab6feb.png&quot; alt=&quot;lc&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;perspective-origin: right center&lt;/strong&gt; 的效果：&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.loli.net/2019/01/26/5c4c1c4a7c0bf.png&quot; alt=&quot;rc&quot; /&gt;&lt;/p&gt;

&lt;h5 id=&quot;backface-visibility&quot;&gt;backface-visibility&lt;/h5&gt;

&lt;p&gt;翻译过来叫背面是否可见，可以设置&lt;code class=&quot;highlighter-rouge&quot;&gt;visible&lt;/code&gt;和&lt;code class=&quot;highlighter-rouge&quot;&gt;hidden&lt;/code&gt;，默认可见，比如元素正面有文字，设置背面可见，则关于y轴旋转180°后元素内文字变成镜像，否则不会出现；&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;backface-visibility: visible&lt;/strong&gt; 的效果：&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.loli.net/2019/01/26/5c4c1d8b0651d.png&quot; alt=&quot;vi&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;backface-visibility: hidden&lt;/strong&gt; 的效果(有旋转，只是背面不可见，则看不见了)：&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.loli.net/2019/01/26/5c4c1e297689d.png&quot; alt=&quot;hi&quot; /&gt;&lt;/p&gt;

&lt;h4 id=&quot;其他属性&quot;&gt;其他属性&lt;/h4&gt;

&lt;h5 id=&quot;transform-origin&quot;&gt;transform-origin&lt;/h5&gt;

&lt;p&gt;设置2D/3D变化的基准，可以是长度值，也可以是 &lt;code class=&quot;highlighter-rouge&quot;&gt;left, right, top, bottom&lt;/code&gt;，例如：&lt;/p&gt;
&lt;div class=&quot;language-css highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;div&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;c&quot;&gt;/* 关于元素左上角旋转 */&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;transform-origin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;left&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;top&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;transform&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rotate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;30deg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;img src=&quot;https://i.loli.net/2019/01/26/5c4c1ed5f21a6.png&quot; alt=&quot;lt&quot; /&gt;&lt;/p&gt;

&lt;h5 id=&quot;transform-style&quot;&gt;transform-style&lt;/h5&gt;

&lt;p&gt;设置元素如何在3D空间呈现被嵌套的元素，选择是 &lt;code class=&quot;highlighter-rouge&quot;&gt;flat&lt;/code&gt; 和 &lt;code class=&quot;highlighter-rouge&quot;&gt;preserve-3d&lt;/code&gt;，默认&lt;code class=&quot;highlighter-rouge&quot;&gt;flat&lt;/code&gt;；&lt;/p&gt;

&lt;p&gt;这里这么来理解，之前我们对一个元素执行变换时，都是以屏幕所在平面的坐标系在变换，但是元素如果存在子元素的话，&lt;code class=&quot;highlighter-rouge&quot;&gt;transform-style&lt;/code&gt;就是用来确定在哪套坐标系上进行变换，&lt;code class=&quot;highlighter-rouge&quot;&gt;flat&lt;/code&gt;表示任然以屏幕坐标系为基准，&lt;code class=&quot;highlighter-rouge&quot;&gt;preserve-3d&lt;/code&gt;表示以&lt;strong&gt;变换后的父元素所在平面的坐标系&lt;/strong&gt;为基准；&lt;/p&gt;

&lt;div class=&quot;language-css highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nf&quot;&gt;#parent&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;transform-style&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;preserve-3d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;-webkit-transform-style&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;preserve-3d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;transform&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;perspective&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;500&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rotateY&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;45deg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;-webkit-transform&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;perspective&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;500&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rotateY&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;45deg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;#child&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;transform&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;perspective&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;500&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rotateX&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;45deg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;-webkit-transform&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;perspective&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;500&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rotateX&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;45deg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;flat&lt;/strong&gt; 的效果：&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.loli.net/2019/01/26/5c4c238db14da.png&quot; alt=&quot;flat&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;preserve-3d&lt;/strong&gt; 的效果：&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.loli.net/2019/01/26/5c4c238dc1ae1.png&quot; alt=&quot;pre&quot; /&gt;&lt;/p&gt;

&lt;p&gt;例如，父元素绕x轴旋转了45度，并且设置&lt;code class=&quot;highlighter-rouge&quot;&gt;transform-style: preserve-3d&lt;/code&gt;，而嵌套的子元素只绕y轴旋转45度，那么最终效果就是子元素绕父元素平面的y轴旋转了45度，看起来就像先x轴转45度后y轴转45度的效果；&lt;/p&gt;

&lt;p&gt;最后，别忘了为以上所有特性添加浏览器兼容前缀；&lt;/p&gt;

&lt;p&gt;顺便附上一个以上功能综合效果的演示页面，请戳下面：&lt;/p&gt;

&lt;h2 id=&quot;css-3d&quot;&gt;&lt;a href=&quot;http://huangqiyun.top/css3d/&quot;&gt;CSS 3D&lt;/a&gt;&lt;/h2&gt;

            </description>
            <pubDate>Sun, 27 Jan 2019 00:00:00 +0000</pubDate>
            <guid>
                https://knightyun.github.io/2019/01/27/css-transform
            </guid>
        </item>
        
        <item>
            <title>CSS单位与尺寸参数</title>
            <link>
                https://knightyun.github.io/2019/01/27/css-px
            </link>
            <description>
                &lt;h4 id=&quot;css单位&quot;&gt;css单位&lt;/h4&gt;

&lt;h5 id=&quot;px&quot;&gt;px&lt;/h5&gt;
&lt;p&gt;常用的单位，即像素pixel缩写，但通常被当做绝对单位，但严格说并不是，因为官方考虑到观看不同设备显示屏时，使网页设计出的某一图形的显示大小在人眼中的观看效果差不多，而定义的一个相对值，即人以一臂之遥观看96DPI的显示屏的角度，大概就是利用透视的近大远小原理，照顾不同设备的最终观看效果。&lt;/p&gt;

&lt;p&gt;比如某网页图形设置为一固定的px值，在手机浏览器上显示是用直尺测大概1cm，但是同样在不缩放情况下，电脑显示屏测量可能就是1.5cm左右，如果是打印机打印出来的话也许就是2cm左右了。&lt;/p&gt;

&lt;h5 id=&quot;em&quot;&gt;em&lt;/h5&gt;
&lt;p&gt;常用的相对单位，前面的数字是比例，即相对于&lt;strong&gt;父元素&lt;/strong&gt;的字体尺寸的比例，比如父元素字体16px，子元素设置1em，也可以理解为100%，那么子元素也是16px，同样，2em就是200%，32px，也可以是小数0.2em，1.5em等等。&lt;/p&gt;

&lt;h5 id=&quot;rem&quot;&gt;rem&lt;/h5&gt;
&lt;p&gt;类似于em，但rem是相对于&lt;strong&gt;根元素html&lt;/strong&gt;，例如用css标签选择器给html标签设置字体尺寸font-size大小为20px，那么文档中的每个1rem就代表20px，1.5em代表30px，以此类推。&lt;/p&gt;

&lt;h5 id=&quot;incmmm&quot;&gt;in，cm，mm&lt;/h5&gt;
&lt;p&gt;这些虽然是生活中的物体测量单位，但网页的1cm尺寸的元素显示到显示器上，用直尺测量通常不是标准1cm，因为css已经默认设置1in=96px，前面也讲过px会因显示屏而不同，因此最终尺寸也不是绝对的，其他也差不多，所以这类尺寸很少用。&lt;/p&gt;

&lt;p&gt;尺寸比较：&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.loli.net/2019/01/26/5c4c2fdf10206.png&quot; alt=&quot;size&quot; /&gt;&lt;/p&gt;

&lt;h4 id=&quot;显示原理&quot;&gt;显示原理&lt;/h4&gt;

&lt;h5 id=&quot;dpi-ppi&quot;&gt;dpi, ppi&lt;/h5&gt;
&lt;p&gt;dpi(dot per inch)，即每英寸多少点，是针对打印机的一个概念，点可以理解为墨点；&lt;/p&gt;

&lt;p&gt;ppi(pixel per inch)，即每英寸多少像素，是针对显示器的概念，开发中一般关心显示器问题，所以一般认为&lt;code class=&quot;highlighter-rouge&quot;&gt;dpi&lt;/code&gt;和&lt;code class=&quot;highlighter-rouge&quot;&gt;ppi&lt;/code&gt;是同一个概念；&lt;/p&gt;

&lt;h5 id=&quot;dpr&quot;&gt;dpr&lt;/h5&gt;
&lt;p&gt;dpr(device pixel ratio)，即物理像素与独立像素的比例；&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;物理像素&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;物理像素&lt;/strong&gt;也叫&lt;strong&gt;设备像素&lt;/strong&gt;，屏幕显示图像都是由很多个像素点组成，屏幕出厂时本身带的点阵数就是它的物理像素；&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;独立像素&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;而&lt;strong&gt;独立像素&lt;/strong&gt;，又叫&lt;strong&gt;逻辑像素&lt;/strong&gt;，或者&lt;strong&gt;css像素&lt;/strong&gt;，顾名思义，逻辑嘛，当然是独立于物理的概念，大小没有固定实际值，也是前面提到的css里面的&lt;code class=&quot;highlighter-rouge&quot;&gt;1px&lt;/code&gt;；&lt;/p&gt;

&lt;p&gt;逻辑像素可以通过js代码获取，&lt;code class=&quot;highlighter-rouge&quot;&gt;screen.width&lt;/code&gt;获取逻辑像素宽度，&lt;code class=&quot;highlighter-rouge&quot;&gt;screen.height&lt;/code&gt;是高度；至于&lt;code class=&quot;highlighter-rouge&quot;&gt;dpr&lt;/code&gt;，可以通过&lt;code class=&quot;highlighter-rouge&quot;&gt;devicePixelRatio&lt;/code&gt;这个全局属性获取，现在的新一些的安卓智能手机一般这个比例是&lt;strong&gt;3&lt;/strong&gt;，电脑的一般是&lt;strong&gt;1&lt;/strong&gt;，iphone、iPad一般是&lt;strong&gt;2&lt;/strong&gt;；&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;注意&lt;/strong&gt;，dpr为&lt;strong&gt;1&lt;/strong&gt;，说明一个css像素块由&lt;code class=&quot;highlighter-rouge&quot;&gt;1x1&lt;/code&gt;个物理像素块来显示，也就是一个物理像素，如果为&lt;strong&gt;2&lt;/strong&gt;，则是一个css像素由&lt;code class=&quot;highlighter-rouge&quot;&gt;2x2&lt;/code&gt;个物理像素来显示，也就是&lt;strong&gt;4&lt;/strong&gt;个像素块，以此类推；这里也就能明白了，dpr的存在就是为了是小尺寸设备屏幕显示出高画质图形，细节更高就更清晰了；&lt;/p&gt;

&lt;p&gt;现在来说设备像素的获取，并没有直接的获取方法，所以可以通过&lt;strong&gt;dpr乘以逻辑像素的方法获取&lt;/strong&gt;；&lt;/p&gt;

&lt;p&gt;综合举例：&lt;/p&gt;
&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;logicWidth&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;screen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;width&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;logicHeight&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;screen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;height&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;dpr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;devicePixelRatio&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;deviceWidth&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;logicWidth&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;dpr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;deviceHeight&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;logicHeight&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;dpr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// 输出逻辑像素&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&#39;逻辑像素：&#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;logicWidth&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&#39;x&#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;logicHeight&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// 输出设备像素&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&#39;物理像素：&#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;deviceWidth&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&#39;x&#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;deviceHeight&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

            </description>
            <pubDate>Sun, 27 Jan 2019 00:00:00 +0000</pubDate>
            <guid>
                https://knightyun.github.io/2019/01/27/css-px
            </guid>
        </item>
        
        <item>
            <title>用CSS画一些多边形状</title>
            <link>
                https://knightyun.github.io/2019/01/27/css-draw-polygon
            </link>
            <description>
                &lt;p&gt;CSS是个很强大的网页开发工具，使生硬的网页变得丰富绚丽，css能实现很多效果，比如css3中的过渡与动画效果都很好看，最基本的就是画一个具有长宽的矩形，通过设置 &lt;code class=&quot;highlighter-rouge&quot;&gt;border-radius&lt;/code&gt; 又能实现画圆形和椭圆形，但是其他多边形似乎没有直接能用的属性，比如&lt;strong&gt;三角形&lt;/strong&gt;，&lt;strong&gt;五角星&lt;/strong&gt;，&lt;strong&gt;六边形&lt;/strong&gt;等等；&lt;/p&gt;

&lt;p&gt;下面根据几何顺序依次来实现一下：&lt;/p&gt;

&lt;h3 id=&quot;圆形&quot;&gt;圆形&lt;/h3&gt;

&lt;html&gt;
&lt;div style=&quot;border:1px solid green;
padding:10px;&quot;&gt;
    &lt;div style=&quot;width:40px;
    height:40px;
    background:red;
    border-radius:20px;&quot;&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;blockquote&gt;
  &lt;h4 id=&quot;分析&quot;&gt;分析:&lt;/h4&gt;
&lt;/blockquote&gt;

&lt;p&gt;在长宽相等的正方形中使用 &lt;code class=&quot;highlighter-rouge&quot;&gt;border-radius&lt;/code&gt; 属性，其值等于长或宽的一半；&lt;/p&gt;

&lt;p&gt;代码：&lt;/p&gt;

&lt;div class=&quot;language-html highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;title&amp;gt;&lt;/span&gt;CSS&lt;span class=&quot;nt&quot;&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;style&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;nt&quot;&gt;div&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;nl&quot;&gt;width&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;40px&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;nl&quot;&gt;height&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;40px&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;nl&quot;&gt;background&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;red&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;nl&quot;&gt;border-radius&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;20px&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;div&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;椭圆形&quot;&gt;椭圆形&lt;/h3&gt;

&lt;html&gt;
&lt;div style=&quot;border:1px solid green;
padding:10px;&quot;&gt;
    &lt;div style=&quot;width:60px;
    height:40px;
    background:red;
    border-radius:50%&quot;&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;blockquote&gt;
  &lt;h4 id=&quot;分析-1&quot;&gt;分析&lt;/h4&gt;
&lt;/blockquote&gt;

&lt;p&gt;同样是 &lt;code class=&quot;highlighter-rouge&quot;&gt;border-radius&lt;/code&gt; 属性，只不过其值有变化，使用 &lt;code class=&quot;highlighter-rouge&quot;&gt;border-radius: 30px/20px&lt;/code&gt;，意思是原矩形宽度方向半径设为 30px，高度方向半径设为 20px，或者简写为 &lt;code class=&quot;highlighter-rouge&quot;&gt;boder-radius: 50%&lt;/code&gt;，一个意思，宽度和高度方向的半径各位宽度和高度的一半；&lt;/p&gt;

&lt;p&gt;代码：&lt;/p&gt;
&lt;div class=&quot;language-html highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;title&amp;gt;&lt;/span&gt;CSS&lt;span class=&quot;nt&quot;&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;style&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;nt&quot;&gt;div&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;nl&quot;&gt;width&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;40px&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;nl&quot;&gt;height&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;40px&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;nl&quot;&gt;background&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;red&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;nl&quot;&gt;border-radius&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;30px&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;20px&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;c&quot;&gt;/* 或者这样
            border-radius: 50%;
            */&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;div&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;三边形&quot;&gt;三边形&lt;/h3&gt;

&lt;html&gt;
&lt;div style=&quot;border:1px solid green;
padding:10px;&quot;&gt;
    &lt;div style=&quot;width:30px;
    height:40px;
    border-left:20px solid transparent;
    border-right:20px solid transparent;
    border-bottom:40px solid red;&quot;&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;blockquote&gt;
  &lt;h4 id=&quot;分析-2&quot;&gt;分析&lt;/h4&gt;
&lt;/blockquote&gt;

&lt;p&gt;没有直接能用的三角形的属性，可以利用CSS的&lt;strong&gt;盒子模型&lt;/strong&gt;，就是下面这种，像 &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;p&amp;gt;, &amp;lt;h1&amp;gt;, &amp;lt;div&amp;gt;&lt;/code&gt; 这些标签都是一个“盒子”，标签内的文本是内容区，周围的彩色边界设置的是 &lt;code class=&quot;highlighter-rouge&quot;&gt;border&lt;/code&gt; 值，当然还有边界与内容区中间的 &lt;code class=&quot;highlighter-rouge&quot;&gt;padding&lt;/code&gt; 值，以及边界外的 &lt;code class=&quot;highlighter-rouge&quot;&gt;margin&lt;/code&gt; 值；&lt;/p&gt;

&lt;html&gt;
&lt;div style=&quot;border:1px solid green;
padding:10px&quot;&gt;
    &lt;div style=&quot;width: 60px;
    height: 40px;
    background: red;
    color: white;
    border-top:10px solid green;
    border-bottom: 10px solid blue;
    border-left: 15px solid yellow;
    border-right: 15px solid aqua&quot;&gt;内容&lt;/div&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;所以由图就能想到办法了，就是让某一条边界的宽度值直接等于盒子的宽度，并设置一个边界颜色，其他边界线设置不同的宽度值来调整三角形的斜度，并把边界线颜色设置为透明 &lt;code class=&quot;highlighter-rouge&quot;&gt;transparent&lt;/code&gt; 即可；&lt;/p&gt;

&lt;p&gt;代码：&lt;/p&gt;
&lt;div class=&quot;language-html highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;title&amp;gt;&lt;/span&gt;CSS&lt;span class=&quot;nt&quot;&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;style&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;nt&quot;&gt;div&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;nl&quot;&gt;width&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;30px&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;nl&quot;&gt;height&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;40px&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;nl&quot;&gt;border-left&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;20px&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;solid&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;transparent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;nl&quot;&gt;border-right&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;20px&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;solid&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;transparent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;nl&quot;&gt;border-bottom&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;40px&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;solid&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;red&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;div&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;矩形&quot;&gt;矩形&lt;/h3&gt;

&lt;html&gt;
&lt;div style=&quot;border:1px solid green;
padding:10px&quot;&gt;
    &lt;div style=&quot;width: 60px;
    height: 40px;
    background: red;&quot;&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;最简单的形状，就不分析了；&lt;/p&gt;

&lt;h3 id=&quot;梯形&quot;&gt;梯形&lt;/h3&gt;

&lt;html&gt;
&lt;div style=&quot;border:1px solid green;
padding:10px&quot;&gt;
    &lt;div style=&quot;width: 60px;
    height: 40px;
    border-left: 10px solid transparent;
    border-right: 10px solid transparent;
    border-bottom: 40px solid red;&quot;&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;blockquote&gt;
  &lt;h4 id=&quot;分析-3&quot;&gt;分析&lt;/h4&gt;
&lt;/blockquote&gt;

&lt;p&gt;方法有些像三角形，只不过底部边界线宽度等于矩形高度，左右两边的边界线宽度小于矩形宽度值即可（感觉这两句话绕就比划着再读几遍 -_-）;&lt;/p&gt;

&lt;p&gt;代码：&lt;/p&gt;
&lt;div class=&quot;language-html highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;title&amp;gt;&lt;/span&gt;CSS&lt;span class=&quot;nt&quot;&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;style&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;nt&quot;&gt;div&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;nl&quot;&gt;width&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;60px&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;nl&quot;&gt;height&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;40px&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;nl&quot;&gt;border-left&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;10px&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;solid&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;transparent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;nl&quot;&gt;border-right&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;10px&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;solid&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;transparent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;nl&quot;&gt;border-bottom&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;40px&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;solid&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;red&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;div&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;平行四边形&quot;&gt;平行四边形&lt;/h3&gt;

&lt;html&gt;
&lt;div style=&quot;border:1px solid green;
padding:10px&quot;&gt;
    &lt;div style=&quot;display: inline-block;
    width: 60px;
    height: 40px;
    border-left: 10px solid transparent;
    border-bottom: 40px solid red;&quot;&gt;
    &lt;/div&gt;&lt;div style=&quot;display: inline-block;
    width: 10px;
    height: 40px;
    border-left: 10px solid red;
    border-bottom: 40px solid transparent;&quot;&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;blockquote&gt;
  &lt;h4 id=&quot;分析-4&quot;&gt;分析&lt;/h4&gt;
&lt;/blockquote&gt;

&lt;p&gt;看成一个倾斜过的矩形，所以可以使用 &lt;code class=&quot;highlighter-rouge&quot;&gt;transform: skew()&lt;/code&gt; 属性，括号内是倾斜角度，比如30度就是 &lt;code class=&quot;highlighter-rouge&quot;&gt;30deg&lt;/code&gt;，还有 &lt;code class=&quot;highlighter-rouge&quot;&gt;transform&lt;/code&gt; 是CSS3中的一个新属性，所以需要加浏览器前缀进行兼容，例如 ：&lt;/p&gt;

&lt;div class=&quot;language-css highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;div&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;transform&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;skew&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;30deg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;-webkit-transform&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;skew&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;30deg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;-moz-transform&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;skew&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;30deg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;-ms-transform&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;skew&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;30deg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;-o-transform&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;skew&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;30deg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;另外脑洞够大可以发挥一下想象，平行四边形可以看成一个直角梯形与一个直角三角形的组合，或者一个矩形与两个直角三角形的组合；&lt;/p&gt;

&lt;p&gt;直角梯形与直角三角形组合的代码：&lt;/p&gt;
&lt;div class=&quot;language-html highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;title&amp;gt;&lt;/span&gt;CSS&lt;span class=&quot;nt&quot;&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;style&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;nf&quot;&gt;#div0&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;nl&quot;&gt;display&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;inline-block&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;nl&quot;&gt;width&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;60px&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;nl&quot;&gt;height&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;40px&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;nl&quot;&gt;border-left&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;10px&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;solid&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;transparent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;nl&quot;&gt;border-bottom&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;40px&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;solid&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;red&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;nf&quot;&gt;#div1&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;nl&quot;&gt;display&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;inline-block&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;nl&quot;&gt;width&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;10px&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;nl&quot;&gt;height&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;40px&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;nl&quot;&gt;border-left&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;10px&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;solid&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;red&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;nl&quot;&gt;border-bottom&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;40px&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;solid&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;transparent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;div&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;id=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;div0&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;&lt;span class=&quot;c&quot;&gt;&amp;lt;!--
    --&amp;gt;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;div&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;id=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;div1&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;注意：&lt;/strong&gt; 两个 &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;div&amp;gt;&lt;/code&gt; 标签之间如果有&lt;strong&gt;换行&lt;/strong&gt;或者空格的话，最终两个块图形间会出现一条&lt;strong&gt;细缝&lt;/strong&gt;，所以写的时候就要避免换行，或者像上面一样把换行&lt;strong&gt;注释掉&lt;/strong&gt;；&lt;/p&gt;

&lt;h3 id=&quot;五边形&quot;&gt;五边形&lt;/h3&gt;

&lt;html&gt;
&lt;div style=&quot;border:1px solid green;
padding:10px&quot;&gt;
    &lt;div style=&quot;width: 50px;
    height: 10px;
    border-left: 25px solid transparent;
    border-right: 25px solid transparent;
    border-bottom: 10px solid red;&quot;&gt;
    &lt;/div&gt;&lt;div style=&quot;width: 50px;
    height: 40px;
    border-top: 40px solid red;
    border-left: 10px solid transparent;
    border-right: 10px solid transparent;&quot;&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;blockquote&gt;
  &lt;p&gt;分析&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;五边形可以看成上面的三角形与下面梯形的组合，当然数学好的可以计算一下尺寸就能画出一个正五边形了；&lt;/p&gt;

&lt;p&gt;代码：&lt;/p&gt;
&lt;div class=&quot;language-html highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;title&amp;gt;&lt;/span&gt;CSS&lt;span class=&quot;nt&quot;&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;style&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;nf&quot;&gt;#div0&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;nl&quot;&gt;width&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;50px&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;nl&quot;&gt;height&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;10px&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;nl&quot;&gt;border-left&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;25px&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;solid&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;transparent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;nl&quot;&gt;border-right&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;25px&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;solid&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;transparent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;nl&quot;&gt;border-bottom&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;10px&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;solid&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;red&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;nf&quot;&gt;#div1&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;nl&quot;&gt;width&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;50px&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;nl&quot;&gt;height&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;40px&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;nl&quot;&gt;border-top&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;40px&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;solid&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;red&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;nl&quot;&gt;border-left&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;10px&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;solid&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;transparent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;nl&quot;&gt;border-right&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;10px&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;solid&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;transparent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;div&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;id=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;div0&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;div&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;id=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;div1&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;更多边的形状基本思路都一样，想着用三角形，矩形，梯形这些基本形象进行组合基本上都能实现，下面的形状就只放形状和源码了；&lt;/p&gt;

&lt;h3 id=&quot;五角星&quot;&gt;五角星&lt;/h3&gt;

&lt;html&gt;
&lt;div style=&quot;border:1px solid green;
padding:10px&quot;&gt;
    &lt;div style=&quot;width: 40px;
    height: 60px;
    border-left: 20px solid transparent;
    border-right: 20px solid transparent;
    border-bottom: 60px solid red;&quot;&gt;
        &lt;div style=&quot;height: 20px;&quot;&gt;&lt;/div&gt;
        &lt;div style=&quot;width: 60px;
        height: 18px;
        border-top: 25px solid red;
        border-left: 30px solid transparent;
        border-right: 30px solid transparent;
        margin-left: -30px;&quot;&gt;&lt;/div&gt;
        &lt;div style=&quot;width: 40px;
        height: 15px;
        border-left: 20px solid transparent;
        border-right: 20px solid transparent;
        border-bottom: 15px solid white;
        margin-left: -20px;&quot;&gt;&lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;代码：&lt;/p&gt;
&lt;div class=&quot;language-html highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;title&amp;gt;&lt;/span&gt;CSS&lt;span class=&quot;nt&quot;&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;style&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;nf&quot;&gt;#div0&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;nl&quot;&gt;width&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;40px&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;nl&quot;&gt;height&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;60px&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;nl&quot;&gt;border-left&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;20px&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;solid&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;transparent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;nl&quot;&gt;border-right&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;20px&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;solid&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;transparent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;nl&quot;&gt;border-bottom&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;60px&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;solid&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;red&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;nf&quot;&gt;#div1&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;nl&quot;&gt;height&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;20px&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;  
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;nf&quot;&gt;#div2&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;nl&quot;&gt;width&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;60px&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;nl&quot;&gt;height&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;18px&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;nl&quot;&gt;border-top&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;25px&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;solid&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;red&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;nl&quot;&gt;border-left&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;30px&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;solid&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;transparent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;nl&quot;&gt;border-right&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;30px&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;solid&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;transparent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;nl&quot;&gt;margin-left&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;-30px&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;nf&quot;&gt;#div3&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;nl&quot;&gt;width&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;40px&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;nl&quot;&gt;height&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;15px&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;nl&quot;&gt;border-left&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;20px&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;solid&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;transparent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;nl&quot;&gt;border-right&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;20px&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;solid&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;transparent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;nl&quot;&gt;border-bottom&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;15px&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;solid&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;white&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;nl&quot;&gt;margin-left&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;-20px&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;div&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;id=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;div0&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;nt&quot;&gt;&amp;lt;div&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;id=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;div1&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;nt&quot;&gt;&amp;lt;div&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;id=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;div2&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;nt&quot;&gt;&amp;lt;div&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;id=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;div3&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;六边形&quot;&gt;六边形&lt;/h3&gt;

&lt;html&gt;
&lt;div style=&quot;border:1px solid green;
padding:10px&quot;&gt;
    &lt;div style=&quot;width: 60px;
    height: 30px;
    border-left: 15px solid transparent;
    border-right: 15px solid transparent;
    border-bottom: 30px solid red;&quot;&gt;&lt;/div&gt;
    &lt;div style=&quot;width: 60px;
    height: 30px;
    border-top: 30px solid red;
    border-left: 15px solid transparent;
    border-right: 15px solid transparent;&quot;&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;代码：&lt;/p&gt;
&lt;div class=&quot;language-html highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;title&amp;gt;&lt;/span&gt;CSS&lt;span class=&quot;nt&quot;&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;style&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;nf&quot;&gt;#div0&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;nl&quot;&gt;width&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;60px&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;nl&quot;&gt;height&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;30px&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;nl&quot;&gt;border-left&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;15px&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;solid&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;transparent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;nl&quot;&gt;border-right&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;15px&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;solid&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;transparent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;nl&quot;&gt;border-bottom&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;30px&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;solid&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;red&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;nf&quot;&gt;#div1&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;nl&quot;&gt;width&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;60px&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;nl&quot;&gt;height&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;30px&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;nl&quot;&gt;border-top&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;30px&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;solid&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;red&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;nl&quot;&gt;border-left&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;15px&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;solid&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;transparent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;nl&quot;&gt;border-right&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;15px&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;solid&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;transparent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;div&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;id=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;div0&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;div&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;id=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;div1&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;当然网页上画像上面这种基本图形，或者跟复杂的几何图形，曲线图形等，多半用到&lt;strong&gt;canvas&lt;/strong&gt;或者&lt;strong&gt;SVG&lt;/strong&gt;这两个工具，功能很强大，可以自行了解；&lt;/p&gt;

            </description>
            <pubDate>Sun, 27 Jan 2019 00:00:00 +0000</pubDate>
            <guid>
                https://knightyun.github.io/2019/01/27/css-draw-polygon
            </guid>
        </item>
        
        <item>
            <title>Base64码简介</title>
            <link>
                https://knightyun.github.io/2019/01/27/base64
            </link>
            <description>
                &lt;h4 id=&quot;简介&quot;&gt;简介&lt;/h4&gt;

&lt;p&gt;base64是一个保存二进制数据的工具，将多种形式的二进制数据或其构成的文件以ASCII的形式保存，因为很多地方不支持直接的二进制文件保存或呈现，比如可以将图片直接转换成base64码嵌入HTML文档中，而避免使用网络http加载图片；&lt;/p&gt;

&lt;h4 id=&quot;组成&quot;&gt;组成&lt;/h4&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;A-Z a-z 0-9 + /&lt;/code&gt; 共64个字符(不信自己数一下);&lt;/p&gt;

&lt;h4 id=&quot;转换&quot;&gt;转换&lt;/h4&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;如果是图片直接转换成二进制文件，字符先转换成ASCII字符码，再转换成二进制；网上直接搜“base64转换工具”就会出来很多在线转换网站，上传图片或者输入文本，一键转换；&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;将上面的64个字符按上面顺序排列得到64个索引，索引从&lt;strong&gt;0&lt;/strong&gt;开始，&lt;strong&gt;63&lt;/strong&gt;结束；&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;因为 64 = 2^6，所以要使二进制包含64种不同情况，需要取6比特位，即&lt;code class=&quot;highlighter-rouge&quot;&gt;000000&lt;/code&gt;这种，把之前文件转换得到的二进制数据按每&lt;strong&gt;6比特位&lt;/strong&gt;取一次，然后得到一个6位二进制数再转换成相应十进制数，这个数就是索引，然后按照索引取相应的64字符中的某一个，最后把所有取得的字符连接就是base64码；&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;编码需要原文本总字节数(字符数)能&lt;strong&gt;被3整除&lt;/strong&gt;(字节数除以3无余数)，因此取到最后的字符时如果凑不够，缺位全部用 &lt;code class=&quot;highlighter-rouge&quot;&gt;0&lt;/code&gt; 补齐，最后的只要是&lt;strong&gt;全为0的6比特位&lt;/strong&gt;，全部转换成字符 &lt;strong&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;=&lt;/code&gt;&lt;/strong&gt;，所以最多会出现&lt;strong&gt;2个 =&lt;/strong&gt;；&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h4 id=&quot;使用&quot;&gt;使用&lt;/h4&gt;

&lt;p&gt;假设一个 .png 图片转换得到的base64码为 &lt;code class=&quot;highlighter-rouge&quot;&gt;abcdefg&lt;/code&gt;，标准格式为：&lt;/p&gt;
&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;data:image/png;base64,abcdefg
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;在浏览器地址栏输入以上字符串回车就能看见图片了，一般浏览器都支持解析base64码(里面的base64码换成自己的)；&lt;/p&gt;

&lt;p&gt;或者用在html的 &lt;code class=&quot;highlighter-rouge&quot;&gt;img&lt;/code&gt; 标签中：&lt;/p&gt;
&lt;div class=&quot;language-html highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;img&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;alt=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;src=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;data:image/png;base64,abcdefg&quot;&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;再或者用在markdown格式文本中：&lt;/p&gt;
&lt;div class=&quot;language-markdown highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;![&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;base64&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;](&lt;/span&gt;&lt;span class=&quot;sx&quot;&gt;data:image/png;base53,abcdefg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;可以把 &lt;code class=&quot;highlighter-rouge&quot;&gt;data:&lt;/code&gt; 看成像 &lt;code class=&quot;highlighter-rouge&quot;&gt;http:&lt;/code&gt; 一样的一种协议，下面是其他格式，根据格式应该就能猜到其用途：&lt;/p&gt;
&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;data:,文本数据
data:text/plain,文本数据
data:text/html,HTML代码
data:text/html;base64,base64编码的HTML代码
data:text/css,CSS代码
data:text/css;base64,base64编码的CSS代码
data:text/javascript,Javascript代码
data:text/javascript;base64,base64编码的Javascript代码
data:image/gif;base64,base64编码的gif图片数据
data:image/png;base64,base64编码的png图片数据
data:image/jpeg;base64,base64编码的jpeg图片数据
data:image/x-icon;base64,base64编码的icon图片数据
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;效果&quot;&gt;效果&lt;/h4&gt;

&lt;p&gt;例如下面的星星图标：&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;data:image/jpeg;base64,iVBORw0KGgoAAAANSUhEUgAAABIAAAASCAQAAAD8x0bcAAAA2UlEQVR4AZWSAQZCQRCGv05QgXpQqCggukAgQeSpEwREoO4Qqg5RoYA6T6GqIIACvKqMMdZK9C1r5t+dmbE7/E+SKj26nz3BV9IseTlrSQqPHGc9jIjUOlPAIeBiGdZszD4SYCycMjXqjjdHifM0cUuMGAfzn8QRQnGGdAgpAlAipMNQgtsIfYnZkcUly070PsJAU18pg1LmquoAoWUdjEAZm9byG28AFSpA028cZioFTHkQMSGjysx/zBsnK3Li/tkvpHDI2wVb7MnjEbD6+cFGQkalZ6PyD2/u1Ikpara+FgAAAABJRU5ErkJggg==&quot; alt=&quot;base64_img&quot; /&gt;&lt;/p&gt;

&lt;p&gt;其实它的base64码是这样的：&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt; iVBORw0KGgoAAAANSUhEUgAAABIAAAASCAQAAAD8x0bcAA
 AA2UlEQVR4AZWSAQZCQRCGv05QgXpQqCggukAgQeSpEwRE
 oO4Qqg5RoYA6T6GqIIACvKqMMdZK9C1r5t+dmbE7/E+SKj
 26nz3BV9IseTlrSQqPHGc9jIjUOlPAIeBiGdZszD4SYCyc
 MjXqjjdHifM0cUuMGAfzn8QRQnGGdAgpAlAipMNQgtsIfY
 nZkcUly070PsJAU18pg1LmquoAoWUdjEAZm9byG28AFSpA
 028cZioFTHkQMSGjysx/zBsnK3Li/tkvpHDI2wVb7MnjEb
 D6+cFGQkalZ6PyD2/u1Ikpara+FgAAAABJRU5ErkJggg==
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;为了显示方便所以进行了换行，理论上是很长的&lt;strong&gt;一行&lt;/strong&gt;连续文本，中间不能有空格或者换行，这个图片仅有274字节(Byte)大，所以base64码还算短，几十上百K的图片就很长了，&lt;strong&gt;100K&lt;/strong&gt;图片就有&lt;strong&gt;13万&lt;/strong&gt;多个字符；&lt;/p&gt;

&lt;p&gt;如果是文本的话，例如：&lt;/p&gt;
&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Hello World
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;以字符串 &lt;code class=&quot;highlighter-rouge&quot;&gt;Hello &lt;/code&gt; 举例，后面类推，先将每个字符转换成ASCII码，再转换成二进制：&lt;/p&gt;
&lt;style&gt;
table th, table td {
    border: 1px solid black;
    text-align: center;
}
&lt;/style&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;字符&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;H&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;e&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;l&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;l&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;o&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;空格&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;strong&gt;ASCII&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;72&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;101&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;108&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;108&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;111&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;32&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;strong&gt;二进制&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;01001000&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;01100101&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;01101100&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;01101100&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;01101111&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;01000000&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;以字符串 &lt;code class=&quot;highlighter-rouge&quot;&gt;Hel&lt;/code&gt; 为例，将二进制转化成base64码：&lt;/p&gt;
&lt;style&gt;
table th, table td {
    border: 1px solid;
    text-align: center;
}
&lt;/style&gt;

&lt;table style=&quot;text-align: center&quot;&gt;
    &lt;tr&gt;
        &lt;th&gt;字符&lt;/th&gt;
        &lt;th colspan=&quot;8&quot;&gt;H&lt;/th&gt;
        &lt;th colspan=&quot;8&quot;&gt;e&lt;/th&gt;
        &lt;th colspan=&quot;8&quot;&gt;l&lt;/th&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
        &lt;th&gt;二进制&lt;/th&gt;
        &lt;td&gt;0&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;
        &lt;td&gt;1&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;
		&lt;td&gt;0&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;
		&lt;td&gt;0&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;
		&lt;td&gt;0&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;
		&lt;td&gt;1&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
        &lt;th&gt;Base64补0&lt;/th&gt;
        &lt;td&gt;0&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;
        &lt;td&gt;1&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;
		&lt;td&gt;0&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;
		&lt;td&gt;0&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;
		&lt;td&gt;0&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;
		&lt;td&gt;1&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
        &lt;th&gt;Base64结果&lt;/th&gt;
        &lt;td colspan=&quot;6&quot;&gt;S&lt;/td&gt;
        &lt;td colspan=&quot;6&quot;&gt;G&lt;/td&gt;
        &lt;td colspan=&quot;6&quot;&gt;V&lt;/td&gt;
        &lt;td colspan=&quot;6&quot;&gt;s&lt;/td&gt;
    &lt;/tr&gt;
&lt;/table&gt;

&lt;p&gt;上面是不用补码的情况，即字符总数是3的倍数；下面是最后剩两个字符需要补码的情况：&lt;/p&gt;
&lt;table style=&quot;text-align: center&quot;&gt;
    &lt;tr&gt;
        &lt;th&gt;字符&lt;/th&gt;
        &lt;th colspan=&quot;8&quot;&gt;H&lt;/th&gt;
        &lt;th colspan=&quot;8&quot;&gt;e&lt;/th&gt;
        &lt;th colspan=&quot;8&quot;&gt;&lt;/th&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
        &lt;th&gt;二进制&lt;/th&gt;
        &lt;td&gt;0&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;
        &lt;td&gt;1&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;
		&lt;td&gt;0&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;
		&lt;td&gt;0&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;
		&lt;td&gt; &lt;/td&gt;&lt;td&gt; &lt;/td&gt;&lt;td&gt; &lt;/td&gt;&lt;td&gt; &lt;/td&gt;
		&lt;td&gt; &lt;/td&gt;&lt;td&gt; &lt;/td&gt;&lt;td&gt; &lt;/td&gt;&lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
        &lt;th&gt;Base64补0&lt;/th&gt;
        &lt;td&gt;0&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;
        &lt;td&gt;1&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;
		&lt;td&gt;0&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;
		&lt;td&gt;0&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;
		&lt;th&gt;0&lt;/th&gt;&lt;th&gt;0&lt;/th&gt;&lt;th&gt;0&lt;/th&gt;&lt;th&gt;0&lt;/th&gt;
		&lt;th&gt;0&lt;/th&gt;&lt;th&gt;0&lt;/th&gt;&lt;th&gt;0&lt;/th&gt;&lt;th&gt;0&lt;/th&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
        &lt;th&gt;Base64结果&lt;/th&gt;
        &lt;td colspan=&quot;6&quot;&gt;S&lt;/td&gt;
        &lt;td colspan=&quot;6&quot;&gt;G&lt;/td&gt;
        &lt;td colspan=&quot;6&quot;&gt;T&lt;/td&gt;
        &lt;th colspan=&quot;6&quot;&gt;=&lt;/th&gt;
    &lt;/tr&gt;
&lt;/table&gt;

&lt;p&gt;然后是最后只剩一个字符的例子：&lt;/p&gt;
&lt;table style=&quot;text-align: center&quot;&gt;
    &lt;tr&gt;
        &lt;th&gt;字符&lt;/th&gt;
        &lt;th colspan=&quot;8&quot;&gt;H&lt;/th&gt;
        &lt;th colspan=&quot;8&quot;&gt; &lt;/th&gt;
        &lt;th colspan=&quot;8&quot;&gt; &lt;/th&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
        &lt;th&gt;二进制&lt;/th&gt;
        &lt;td&gt;0&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;
        &lt;td&gt;1&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;
		&lt;td&gt; &lt;/td&gt;&lt;td&gt; &lt;/td&gt;&lt;td&gt; &lt;/td&gt;&lt;td&gt; &lt;/td&gt;
		&lt;td&gt; &lt;/td&gt;&lt;td&gt; &lt;/td&gt;&lt;td&gt; &lt;/td&gt;&lt;td&gt; &lt;/td&gt;
		&lt;td&gt; &lt;/td&gt;&lt;td&gt; &lt;/td&gt;&lt;td&gt; &lt;/td&gt;&lt;td&gt; &lt;/td&gt;
		&lt;td&gt; &lt;/td&gt;&lt;td&gt; &lt;/td&gt;&lt;td&gt; &lt;/td&gt;&lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
        &lt;th&gt;Base64补0&lt;/th&gt;
        &lt;td&gt;0&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;
        &lt;td&gt;0&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;
		&lt;th&gt;0&lt;/th&gt;&lt;th&gt;0&lt;/th&gt;&lt;th&gt;0&lt;/th&gt;&lt;th&gt;0&lt;/th&gt;
		&lt;th&gt;0&lt;/th&gt;&lt;th&gt;0&lt;/th&gt;&lt;th&gt;0&lt;/th&gt;&lt;th&gt;0&lt;/th&gt;
		&lt;th&gt;0&lt;/th&gt;&lt;th&gt;0&lt;/th&gt;&lt;th&gt;0&lt;/th&gt;&lt;th&gt;0&lt;/th&gt;
		&lt;th&gt;0&lt;/th&gt;&lt;th&gt;0&lt;/th&gt;&lt;th&gt;0&lt;/th&gt;&lt;th&gt;0&lt;/th&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
        &lt;th&gt;Base64结果&lt;/th&gt;
        &lt;td colspan=&quot;6&quot;&gt;S&lt;/td&gt;
        &lt;th colspan=&quot;6&quot;&gt;A&lt;/th&gt;
        &lt;th colspan=&quot;6&quot;&gt;=&lt;/th&gt;
        &lt;th colspan=&quot;6&quot;&gt;=&lt;/th&gt;
    &lt;/tr&gt;
&lt;/table&gt;

&lt;p&gt;&lt;strong&gt;值得注意&lt;/strong&gt;的是base64码 &lt;code class=&quot;highlighter-rouge&quot;&gt;A&lt;/code&gt; 的二进制也是 &lt;code class=&quot;highlighter-rouge&quot;&gt;000000&lt;/code&gt;，上面例子中 &lt;code class=&quot;highlighter-rouge&quot;&gt;A&lt;/code&gt; 的二进制码是部分补全后全为0，只有6个0全是补全的时候才都转换为字符 &lt;code class=&quot;highlighter-rouge&quot;&gt;=&lt;/code&gt;；&lt;/p&gt;


            </description>
            <pubDate>Sun, 27 Jan 2019 00:00:00 +0000</pubDate>
            <guid>
                https://knightyun.github.io/2019/01/27/base64
            </guid>
        </item>
        
        <item>
            <title>UTF-8, ASCII, unicode的介绍与区分</title>
            <link>
                https://knightyun.github.io/2019/01/27/ascii-unicode
            </link>
            <description>
                &lt;h4 id=&quot;背景&quot;&gt;背景&lt;/h4&gt;

&lt;p&gt;人类能通过肉眼识别文字和字符，并能通过知识了解他们的含义，但是计算机内部不论存储还是控制，都是通过二进制码实现，因为二进制的 &lt;code class=&quot;highlighter-rouge&quot;&gt;0&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;1&lt;/code&gt; 刚好对应基础电路中的&lt;strong&gt;开&lt;/strong&gt;和&lt;strong&gt;关&lt;/strong&gt;，然后组合进行复杂的系统控制；&lt;/p&gt;

&lt;p&gt;将人类识别的字符转换成计算机识别的二进制数据的过程，叫做&lt;strong&gt;编码&lt;/strong&gt;，顾名思义，编程二进制数字码，如 &lt;code class=&quot;highlighter-rouge&quot;&gt;0101101100011001&lt;/code&gt;这样的；相反，就叫做&lt;strong&gt;解码&lt;/strong&gt;，把二进制码解释为字符；&lt;/p&gt;

&lt;h4 id=&quot;ascii&quot;&gt;ASCII&lt;/h4&gt;

&lt;p&gt;首先ASCII全称(American Standard Code for Information Interchange，美国信息交换标准代码)，是一个&lt;strong&gt;字符集&lt;/strong&gt;，顾名思义，很多字符的集合；&lt;/p&gt;

&lt;p&gt;像前面提到的，人类与计算机语言不通，一个识别字符，一个识别二进制，所以&lt;strong&gt;ASCII&lt;/strong&gt;就充当了这样一个&lt;strong&gt;翻译官&lt;/strong&gt;，其内容是编码与字符的映射，即一个字符只对应一个固定的编码，例如字符 &lt;code class=&quot;highlighter-rouge&quot;&gt;A&lt;/code&gt; 的编码为 &lt;code class=&quot;highlighter-rouge&quot;&gt;65&lt;/code&gt;，字符 &lt;code class=&quot;highlighter-rouge&quot;&gt;a&lt;/code&gt; 的编码为 &lt;code class=&quot;highlighter-rouge&quot;&gt;122&lt;/code&gt;；当然这个编码是&lt;strong&gt;十进制&lt;/strong&gt;的，计算机内部把十进制转换成二进制就能供底层使用了；&lt;/p&gt;

&lt;p&gt;另外需要知道的是，一字节(1 Byte)等于八比特位(8 bit)，8 bit就是这样的：&lt;code class=&quot;highlighter-rouge&quot;&gt;01010101&lt;/code&gt;，八位二进制的所有不同表示一共 &lt;strong&gt;2^8 = 256&lt;/strong&gt; 个，而且一般都是从 &lt;strong&gt;0&lt;/strong&gt; 开始数，所以表示的十进制数的范围就是 &lt;strong&gt;0 - 255&lt;/strong&gt;，这也是ASCII编码映射字符数的范围，包含大小写字母和一些其他常用的符号；&lt;/p&gt;

&lt;h4 id=&quot;unicode&quot;&gt;Unicode&lt;/h4&gt;

&lt;p&gt;看完上面肯定就会疑惑ASCII总共才表示256个字符，怎么处理当今世界巨大的信息量的，由于这个字符集最初是老外发明的，表示所有字母和一些字符对他们当时来说可能很足够了，但是先进计算机遍布全球大部分地方，汉语、韩语、日语、阿拉伯语等语言数不过来，所以ASCII明显不够用了；虽然中国之前也制定了是和中文的编码字符集叫 &lt;strong&gt;GB2313&lt;/strong&gt; 等系列；&lt;/p&gt;

&lt;p&gt;因此，便顺应时代需要产生了一种更庞大的字符集叫 &lt;strong&gt;Unicode&lt;/strong&gt;，有时也叫&lt;strong&gt;万国码&lt;/strong&gt;，顾名思义，几乎表示了世界上所有语言的字符，可以理解为 &lt;code class=&quot;highlighter-rouge&quot;&gt;Unique code&lt;/code&gt;，独一无二的的编码；&lt;/p&gt;

&lt;p&gt;目前Unicode的编码范围达到了&lt;strong&gt;21位&lt;/strong&gt;，即 &lt;code class=&quot;highlighter-rouge&quot;&gt;0x0000 - 0x10ffff&lt;/code&gt; 的范围，二进制为 &lt;code class=&quot;highlighter-rouge&quot;&gt;1 0000 1111 1111 1111 1111&lt;/code&gt;，刚好21位；十进制表示为 &lt;code class=&quot;highlighter-rouge&quot;&gt;1114111&lt;/code&gt;，就是一百万多个字符，已经相当多了；&lt;/p&gt;

&lt;p&gt;如果要使用UNIcode，以在 &lt;strong&gt;HTML&lt;/strong&gt; 中为例，假如知道一个字符的Unicode码是 &lt;code class=&quot;highlighter-rouge&quot;&gt;0x0394&lt;/code&gt;,那么就在标签中添加代码：&lt;/p&gt;
&lt;div class=&quot;language-html highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;ni&quot;&gt;&amp;amp;#x0394;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;放在标签中就是：&lt;/p&gt;
&lt;div class=&quot;language-html highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;h5&amp;gt;&lt;/span&gt;这个Unicode码对应的字符是：&lt;span class=&quot;ni&quot;&gt;&amp;amp;#x0394;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;/h5&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;结果是这样：&lt;/p&gt;

&lt;blockquote&gt;
  &lt;h5&gt;这个Unicode码对应的字符是：&amp;#x0394;&lt;/h5&gt;
&lt;/blockquote&gt;

&lt;p&gt;其实那个Unicode编码就是对应的大小希腊字母德尔塔，数学或物理中经常用到的字符；&lt;/p&gt;

&lt;p&gt;也可以用 JavaScript 来遍历一部分Unicode与字符的对应关系：&lt;/p&gt;
&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x0000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x00ff&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;document&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&#39;: &amp;amp;#x&#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&#39;;&amp;lt;br&amp;gt;&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;页面就会出现前256个字符及其Unicode码；&lt;/p&gt;

&lt;h4 id=&quot;utf-8-utf-16-utf-32&quot;&gt;UTF-8, UTF-16, UTF-32&lt;/h4&gt;

&lt;p&gt;首先UTF全称(Unicode Transformation Format)，所以它是一种针对前面提到的Unicode的&lt;strong&gt;编码格式&lt;/strong&gt;，常见的格式就是 &lt;strong&gt;UTF-8&lt;/strong&gt;，还有 &lt;code class=&quot;highlighter-rouge&quot;&gt;UTF-16&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;UTF-32&lt;/code&gt;；&lt;/p&gt;

&lt;p&gt;UTF-8 其中的 &lt;code class=&quot;highlighter-rouge&quot;&gt;8&lt;/code&gt; 表示的是 &lt;code class=&quot;highlighter-rouge&quot;&gt;8 bit&lt;/code&gt;，即Unicode中每8位表示一个字符，UTF-16 和 UTF-32 类似，因为Unicode最多才21位，32位大于21位，所以 UTF-32 的格式就可以表示所有字符对应的Unicode码了，但是呢，32位也就是&lt;strong&gt;4字节&lt;/strong&gt;，让每个字符都占用4字节太费空间了，所以出现了&lt;strong&gt;UTF-8&lt;/strong&gt;和&lt;strong&gt;UTF-16&lt;/strong&gt;;&lt;/p&gt;

&lt;p&gt;UTF-8 定义 &lt;code class=&quot;highlighter-rouge&quot;&gt;0 - 7 bit&lt;/code&gt; 的 Unicode 用一字节表示，这里就与&lt;strong&gt;ASCII&lt;/strong&gt;一样了，&lt;code class=&quot;highlighter-rouge&quot;&gt;8 - 11 bit&lt;/code&gt; 用两字节表示，&lt;code class=&quot;highlighter-rouge&quot;&gt;12 - 16 bit&lt;/code&gt; 用三字节表示，&lt;code class=&quot;highlighter-rouge&quot;&gt;17 - 21 bit&lt;/code&gt; 用四字节表示;&lt;/p&gt;

&lt;p&gt;UTF-8 编码规则如下：&lt;/p&gt;
&lt;style&gt;
table th, table td {
    border: 2px solid black;
}
&lt;/style&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Unicode&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;bit&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;UTF-8&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;byte&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;0x0000 - 0x007f&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;0 - 7&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;0XXX XXXX&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;1&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;0x0080 - 0x07ff&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;8 - 11&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;110X XXXX 10XX XXXX&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;2&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;0x0800 - 0xffff&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;12 - 16&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;1110 XXXX 10XX XXXX 10XX XXXX&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;3&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;0x1 0000 - 0x1f ffff&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;17 - 21&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;1111 0XXX 10XX XXXX 10XX XXXX 10XX XXXX&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;4&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;规律是：&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;每个字节中不足8位的，高位(左边)先用0补上，比如 &lt;code class=&quot;highlighter-rouge&quot;&gt;0XXXX XXXX&lt;/code&gt;；&lt;/li&gt;
  &lt;li&gt;超过两字节表示的UTF-8，第一个字节高位添加两个 &lt;code class=&quot;highlighter-rouge&quot;&gt;1&lt;/code&gt; 和一个 &lt;code class=&quot;highlighter-rouge&quot;&gt;0&lt;/code&gt;，后面的字节高位添加 &lt;code class=&quot;highlighter-rouge&quot;&gt;10&lt;/code&gt;；&lt;/li&gt;
  &lt;li&gt;三、四字节同理，几个字节高位就添几个 &lt;code class=&quot;highlighter-rouge&quot;&gt;1&lt;/code&gt; 再加上一个 &lt;code class=&quot;highlighter-rouge&quot;&gt;0&lt;/code&gt;，其余字节高位添 &lt;code class=&quot;highlighter-rouge&quot;&gt;10&lt;/code&gt;；&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;可以看出 UTF-8 这种针对不同位数使用不同字节数编码的方式有效的利用了空间，避免了一些浪费，当然，事物都有利弊，空间降下去，时间也就升上去了；&lt;/p&gt;

            </description>
            <pubDate>Sun, 27 Jan 2019 00:00:00 +0000</pubDate>
            <guid>
                https://knightyun.github.io/2019/01/27/ascii-unicode
            </guid>
        </item>
        
        <item>
            <title>电脑固态硬盘接口辨析</title>
            <link>
                https://knightyun.github.io/2018/12/08/ssd
            </link>
            <description>
                &lt;p&gt;固态硬盘相比传统机械硬盘读写速度快许多，固态硬盘目前分为M.2接口与SATA接口。&lt;/p&gt;

&lt;h4 id=&quot;sata接口&quot;&gt;SATA接口&lt;/h4&gt;

&lt;p&gt;SATA（Serial ATA，Serial Advanced Technology Attachment，串行高级技术附件），一种硬盘接口规范。&lt;/p&gt;

&lt;p&gt;SATA3是目前大多数笔记本使用的接口规范版本，尺寸比msata大一些，有个硬壳盒包装；&lt;/p&gt;

&lt;p&gt;如下图，就是笔记本上取下的固态：&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.loli.net/2018/12/08/5c0b60b71fa9c.jpg&quot; alt=&quot;sata_one&quot; /&gt;
&lt;img src=&quot;https://i.loli.net/2018/12/08/5c0b60ae9c878.jpg&quot; alt=&quot;sata_two&quot; /&gt;&lt;/p&gt;

&lt;p&gt;PCI-E(PCI Express)新一代总线接口，取代PCI总线接口，称为第三代I/O总线技术，提高了带宽，PCI Express的接口根据总线位宽不同而有所差异，包括x1、x4、x8以及x16(x2)模式将用于内部接口而非插槽模式。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;PCI-E传输速度大于SATA。&lt;/strong&gt;&lt;/p&gt;

&lt;h4 id=&quot;m2接口&quot;&gt;M.2接口&lt;/h4&gt;

&lt;p&gt;M.2是intel推出替代mSATA(mini-SATA，小尺寸SATA规范接口，多应用于笔记本固态硬盘接口)的新接口规范，尺寸更小，传输性能更高，为满足超级本用户而推出；&lt;/p&gt;

&lt;p&gt;M.2接口有两种类型：Socket 2（ngff）和Socket 3（nvme），Socket2支持SATA、PCI-E X2接口，PCI-E ×2接口标准，最大的读取速度可以达到700MB/s，写入达到550MB/s。Socket 3可支持PCI-E ×4接口，理论带宽达4GB/s。&lt;/p&gt;

&lt;p&gt;NVME(Non-Volatile Memory express，非易失性内存主机控制器接口规范)，充分利用PCIE通道的低延时和并行性，降低AHCI的高延时和提高SATA的性能；&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;NVME缺口靠右边，使用一缺固态;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;img src=&quot;https://i.loli.net/2018/12/08/5c0b60b31adf5.jpg&quot; alt=&quot;m.2_1&quot; /&gt;
&lt;img src=&quot;https://i.loli.net/2018/12/08/5c0b60b4589e0.jpg&quot; alt=&quot;m.2_0&quot; /&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;NGFF缺口靠左边，使用两缺m.2固态;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;img src=&quot;https://i.loli.net/2018/12/08/5c0bbc1207a8b.jpg&quot; alt=&quot;c3fb1290842eb6f7.jpg&quot; /&gt;&lt;/p&gt;

&lt;p&gt;购买更换时需要注意主板插槽上缺口靠哪边，一般安装是固态条元器件面朝上。&lt;/p&gt;

&lt;p&gt;并且，不要把它和内存条搞混，区别在于接口位置。&lt;/p&gt;

            </description>
            <pubDate>Sat, 08 Dec 2018 00:00:00 +0000</pubDate>
            <guid>
                https://knightyun.github.io/2018/12/08/ssd
            </guid>
        </item>
        
        <item>
            <title>搭建个人论坛网站图文教程</title>
            <link>
                https://knightyun.github.io/2018/11/25/discuz-website
            </link>
            <description>
                &lt;h4 id=&quot;关于建站&quot;&gt;关于建站&lt;/h4&gt;
&lt;p&gt;前面的文章介绍过如何使用Github Pages提供的仓库服务搭建Jekyll个人博客，链接在这 &lt;a href=&quot;https://knightyun.github.io/2018/04/01/github-pages-blog&quot;&gt;https://knightyun.github.io/2018/04/01/github-pages-blog&lt;/a&gt;，现在来了解一下如何一步步搭建自己的论坛网站。&lt;/p&gt;

&lt;p&gt;搭建个人论坛网站，也相当于建站的一种，通常的套路便是&lt;strong&gt;域名+主机空间+网页源码程序&lt;/strong&gt;，网页文件上传到虚拟空间，一般这些空间都安装有网站所需环境，如PHP和数据库等，然后把域名和主机空间绑定，这样就能在浏览器中输入域名（网址）访问所建网页，不然理论上只能通过虚拟主机的IP访问，但是通常有&lt;strong&gt;IP访问限制&lt;/strong&gt;不允许你这么做。&lt;/p&gt;

&lt;p&gt;域名和主机空间可以在各大网站市场购买，通常是按年或按月付费，如阿里云和华为云。这里说一个需要&lt;strong&gt;注意&lt;/strong&gt;的东西，产品列表中的&lt;strong&gt;虚拟空间、虚拟主机&lt;/strong&gt;这类产品，指的是云服务器分配出的一部分磁盘空间，也叫网站空间，即只能通过服务商提供的控制面板进行访问控制，上传或者下载网页文件；而&lt;strong&gt;云服务器、云主机&lt;/strong&gt;这类名称指的是有独立操作系统的服务器，可理解为一台可远程桌面控制访问的-云电脑。当然他们也能通过价格区分，一般情况，域名几十块每年，空间几百每年，主机几千每年，更高级的企业级服务就更贵了-_-。&lt;/p&gt;

&lt;h4 id=&quot;关于论坛&quot;&gt;关于论坛&lt;/h4&gt;
&lt;p&gt;能力强大的大佬可以自己编写网页和程序文件上传到主机空间，普通选手一般选择使用一些开源的网站源码，网上能找到很多，懂一些网页基础的也可以后期修改一些内部样式效果。&lt;/p&gt;

&lt;p&gt;这里我们使用&lt;strong&gt;Discuz&lt;/strong&gt;提供的论坛建设服务，相当于让你下载一份网站源码压缩包，解压后上传到自己的网页空间，然后安装他们的程序。先看一下其&lt;a href=&quot;http://www.comsenz.com/&quot;&gt;官网&lt;/a&gt;的简介：&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.loli.net/2018/11/25/5bfa5a2311d32.png&quot; alt=&quot;Image Title&quot; /&gt;&lt;/p&gt;

&lt;p&gt;他们提供的服务还是不错的，这个源码也是免费下载的，当然也有其他付费服务可以自行了解，下面是他们提供服务的企业，看样子还是很厉害的：&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.loli.net/2018/11/25/5bfa5b4142af4.png&quot; alt=&quot;Image Title&quot; /&gt;&lt;/p&gt;

&lt;h4 id=&quot;开始安装&quot;&gt;开始安装&lt;/h4&gt;
&lt;p&gt;首先到其官网&lt;a href=&quot;http://www.comsenz.com/downloads/install/discuzx#down_open&quot;&gt;下载页面&lt;/a&gt;，然后选择下载的版本，我们这里选择简体UTF-8，点击下载等待完成：&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.loli.net/2018/11/25/5bfa5f33e7571.png&quot; alt=&quot;Image Title&quot; /&gt;&lt;/p&gt;

&lt;p&gt;也可以到其&lt;a href=&quot;http://www.discuz.net/forum.php&quot;&gt;官方论坛&lt;/a&gt;了解，版本更新就在这里发布，我们去其官方gitee，点击&lt;a href=&quot;https://gitee.com/ComsenzDiscuz/DiscuzX&quot;&gt;下载页面&lt;/a&gt;下载：&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.loli.net/2018/11/25/5bfa872216ff4.png&quot; alt=&quot;Image Title&quot; /&gt;&lt;/p&gt;

&lt;p&gt;如图所示下载zip压缩文件即可。由于作者使用最上面那个下载页面的压缩文件测试不成功，因此使用这个下载页面的压缩包。&lt;/p&gt;

&lt;p&gt;解压后进入&lt;strong&gt;upload&lt;/strong&gt;文件夹，其他都是一些说明性文档，可以自行了解一下：&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.loli.net/2018/11/25/5bfa609973982.png&quot; alt=&quot;Image Title&quot; /&gt;&lt;/p&gt;

&lt;p&gt;然后把upload目录下的所有文件复制到自己购买的空间的网页根目录下，一般是wwwroot这类名字，可以在里面新建一个文件夹例如&lt;strong&gt;discuz&lt;/strong&gt;，然后拷贝到这个文件目录里面，这样就不妨碍以后用虚拟空间制作其他网页。&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.loli.net/2018/11/25/5bfa630914816.png&quot; alt=&quot;Image Title&quot; /&gt;&lt;/p&gt;

&lt;p&gt;其主要文件大概就这些，我这里用自己的虚拟机建设的网站做演示，大家就上传到自己虚拟空间目录下，然后在自己电脑浏览器输入绑定的域名进行访问，关于如何绑定域名，很简单并且网上教程很多，自行了解。我自己电脑作为服务器因此网址如图所示，如果域名为&lt;code class=&quot;highlighter-rouge&quot;&gt;test.com&lt;/code&gt;，并且上面提到的所有文件保存在&lt;code class=&quot;highlighter-rouge&quot;&gt;discuz·里面，那么访问网址就输入&lt;/code&gt;test.com/discuz/`，然后进入安装页面：&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.loli.net/2018/11/25/5bfa838094975.png&quot; alt=&quot;Image Title&quot; /&gt;&lt;/p&gt;

&lt;p&gt;然后点同意，下一步，进入如下页面，选择&lt;strong&gt;全新安装&lt;/strong&gt;：&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.loli.net/2018/11/25/5bfa8b39abfe0.png&quot; alt=&quot;Image Title&quot; /&gt;&lt;/p&gt;

&lt;p&gt;设置数据库，数据库名，用户名，密码，一般买虚拟空间有送一个数据库，没有的话就单独买一个数据库服务器，把地址改一下就行了，邮箱填常用的，用于发送错误报告：&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.loli.net/2018/11/25/5bfa8c222e37b.png&quot; alt=&quot;Image Title&quot; /&gt;&lt;/p&gt;

&lt;p&gt;等待页面安装完成，就能点击访问了：&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.loli.net/2018/11/25/5bfa8e647689c.png&quot; alt=&quot;Image Title&quot; /&gt;&lt;/p&gt;

&lt;p&gt;那些推荐应用可以暂时不管，以后还能安装，直接点击右下方访问，看看效果：&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.loli.net/2018/11/25/5bfa9f083d437.png&quot; alt=&quot;Image Title&quot; /&gt;&lt;/p&gt;

&lt;p&gt;右上方是用户注册和登录的地方，管理员就用刚才安装页面设置那个账号和密码，就是&lt;strong&gt;admin&lt;/strong&gt;那个。点击右上方&lt;strong&gt;模块管理，管理中心&lt;/strong&gt;就可以进入后台管理一个内容和功能了，会再次验证密码，还是刚才那个。&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.loli.net/2018/11/25/5bfaa02b2a22d.png&quot; alt=&quot;Image Title&quot; /&gt;&lt;/p&gt;

&lt;p&gt;整个系统比较庞大，功能模块也相当多，有时间多多摸索一下就能熟练管理了。&lt;/p&gt;

            </description>
            <pubDate>Sun, 25 Nov 2018 00:00:00 +0000</pubDate>
            <guid>
                https://knightyun.github.io/2018/11/25/discuz-website
            </guid>
        </item>
        
        <item>
            <title>JavaScript逻辑运算符“&amp;&amp;”和“||”短路原则的应用</title>
            <link>
                https://knightyun.github.io/2018/06/01/js-logic-compute
            </link>
            <description>
                &lt;h4 id=&quot;逻辑运算符&quot;&gt;逻辑运算符&lt;/h4&gt;

&lt;p&gt;在Javascript中，有逻辑运算符 &lt;code class=&quot;highlighter-rouge&quot;&gt;与 &amp;amp;&amp;amp;&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;或 ||&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;非 !&lt;/code&gt;，常在条件句或循环中进行逻辑判断。&lt;/p&gt;

&lt;p&gt;例如：&lt;/p&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; 
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;nx&quot;&gt;alert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;true&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; 
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;nx&quot;&gt;alert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;false&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;括号中表达式值为&lt;strong&gt;真&lt;/strong&gt;，最后提示“true”。&lt;/p&gt;

&lt;h4 id=&quot;短路原则&quot;&gt;短路原则&lt;/h4&gt;

&lt;p&gt;在逻辑运算中，这是一个通用的原则，这是由于表达式从左到右执行的特性，为了减少运算量而给运算器规定的操作。主要针对 &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;amp;&amp;amp;&lt;/code&gt; 和 &lt;code class=&quot;highlighter-rouge&quot;&gt;||&lt;/code&gt; 两种运算。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;amp;&amp;amp;&lt;/code&gt; 的判断是&lt;strong&gt;同真为真，一假为假&lt;/strong&gt;，则运算如果左边的表达式值为 &lt;code class=&quot;highlighter-rouge&quot;&gt;false&lt;/code&gt;，那么就不会再执行右边的表达式了，如果左表达式为 &lt;code class=&quot;highlighter-rouge&quot;&gt;true&lt;/code&gt;，就会继续执行右表达式；&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;||&lt;/code&gt; 的判断是&lt;strong&gt;一真为真，同假为假&lt;/strong&gt;，则运算如果坐表达式值为 &lt;code class=&quot;highlighter-rouge&quot;&gt;true&lt;/code&gt;，那么就不用执行右边的表达式了，如果左表达式为 &lt;code class=&quot;highlighter-rouge&quot;&gt;false&lt;/code&gt;，就会继续执行右表达式；&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;举例说明：&lt;/p&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;alert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;msg1&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;alert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;msg2&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;alert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;msg3&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;alert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;msg4&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;结果是提示&lt;strong&gt;“msg1”&lt;/strong&gt;和&lt;strong&gt;“msg4”&lt;/strong&gt;。原理如上述。&lt;/p&gt;

&lt;p&gt;因此，如果有以下表达式：&lt;/p&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; 
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;nx&quot;&gt;alert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;true&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; 
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;nx&quot;&gt;alert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;false&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;也许你会使用&lt;strong&gt;三目运算&lt;/strong&gt;简化成这样：&lt;/p&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;alert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;true&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;alert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;false&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;其实也可以这样写：&lt;/p&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;alert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;true&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;alert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;false&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;javascript中的应用&quot;&gt;Javascript中的应用&lt;/h4&gt;

&lt;p&gt;在javascript中，只有&lt;code class=&quot;highlighter-rouge&quot;&gt;对象(Object)&lt;/code&gt;和布尔值&lt;code class=&quot;highlighter-rouge&quot;&gt;true&lt;/code&gt;为真，其它例如 &lt;code class=&quot;highlighter-rouge&quot;&gt;undefined&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;NaN&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;false&lt;/code&gt; 等，值为 &lt;code class=&quot;highlighter-rouge&quot;&gt;false&lt;/code&gt;。为被定义的对象或未赋值变量也是 &lt;code class=&quot;highlighter-rouge&quot;&gt;false&lt;/code&gt;，因为其值都是 &lt;code class=&quot;highlighter-rouge&quot;&gt;undefined&lt;/code&gt;，这里就可以应用于检查某变量是否&lt;strong&gt;已定义&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;举例说明：&lt;/p&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;o&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;alert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;defined&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;       &lt;span class=&quot;c1&quot;&gt;//已定义a，提示“defined”&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;o&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;alert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;defined&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;       &lt;span class=&quot;c1&quot;&gt;//已定义对象o，提示“defined”&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;Object&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;alert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;defined&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;//Object是一个已知的全局对象，提示“defined”&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;alert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;not defined&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;   &lt;span class=&quot;c1&quot;&gt;//b未赋值，提示“not defined”&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;alert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;not defined&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;   &lt;span class=&quot;c1&quot;&gt;//未定义p，提示“not defined”&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;除了这个也能衍生出其它相同原理的应用，类似于判断赋值 &lt;code class=&quot;highlighter-rouge&quot;&gt;var  a = (b &amp;gt; 0) &amp;amp;&amp;amp; &#39;9&#39;&lt;/code&gt; 或判断定义变量 &lt;code class=&quot;highlighter-rouge&quot;&gt;var abc = abc || &quot;&quot;&lt;/code&gt; 等。&lt;/p&gt;

&lt;p&gt;js中使用这种方法可以减少代码量，提示性能，但同时也降低了代码&lt;strong&gt;可读性&lt;/strong&gt;，比如个人觉得还是看以下代码比较舒适：&lt;/p&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;nx&quot;&gt;alert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;true&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;方法的选择就要视情况而权衡了。&lt;/p&gt;

            </description>
            <pubDate>Fri, 01 Jun 2018 00:00:00 +0000</pubDate>
            <guid>
                https://knightyun.github.io/2018/06/01/js-logic-compute
            </guid>
        </item>
        
        <item>
            <title>jQuery初识之安装与语法简介</title>
            <link>
                https://knightyun.github.io/2018/06/01/jq-syntax
            </link>
            <description>
                &lt;h4 id=&quot;概念&quot;&gt;概念&lt;/h4&gt;

&lt;p&gt;jQuery是一个JavaScript&lt;strong&gt;函数库&lt;/strong&gt;，是一个比较流行的&lt;strong&gt;js框架&lt;/strong&gt;，功能就是简化 js 代码的书写，因为一些功能用原生javascript书写代码量是很大的。可以理解为&lt;strong&gt;javascript query&lt;/strong&gt;，毕竟Query也是它的一个功能。&lt;/p&gt;

&lt;h4 id=&quot;安装&quot;&gt;安装&lt;/h4&gt;

&lt;p&gt;要使用jQuery库，可以从网上下载得到jQuery的 &lt;code class=&quot;highlighter-rouge&quot;&gt;.js&lt;/code&gt; 文件，也可以使用&lt;strong&gt;CDN (Content Delivery Content 内容分发网络)&lt;/strong&gt;加载jQuery。&lt;/p&gt;

&lt;h5 id=&quot;下载&quot;&gt;下载&lt;/h5&gt;

&lt;p&gt;需要去jQuery官网：&lt;a href=&quot;http://jquery.com/&quot;&gt;jquery.com&lt;/a&gt; 下载需要的jQuery库，一般有两个版本，&lt;code class=&quot;highlighter-rouge&quot;&gt;production&lt;/code&gt; 表示已被压缩精简的版本，用于放到实际网站中，&lt;code class=&quot;highlighter-rouge&quot;&gt;development&lt;/code&gt; 表示测试开发版，用于编写和开发，是&lt;strong&gt;可读&lt;/strong&gt;的代码。&lt;/p&gt;

&lt;p&gt;例如目前最新的版本是 &lt;code class=&quot;highlighter-rouge&quot;&gt;jquery-3.3.1.js&lt;/code&gt;，压缩版后缀是 &lt;code class=&quot;highlighter-rouge&quot;&gt;.min.js&lt;/code&gt;，开发版文件有一万多行，就是正常格式的JavaScript源代码，包含一些注释，文件大小为 &lt;code class=&quot;highlighter-rouge&quot;&gt;266k&lt;/code&gt;；压缩版就是去掉里面不必要的&lt;strong&gt;空格&lt;/strong&gt;，&lt;strong&gt;回车&lt;/strong&gt;与&lt;strong&gt;注释&lt;/strong&gt;，所以最后文件实际内容只有&lt;strong&gt;一行！&lt;/strong&gt;，文件大小为 &lt;code class=&quot;highlighter-rouge&quot;&gt;85k&lt;/code&gt;，压缩了近&lt;strong&gt;三倍&lt;/strong&gt;，这也是网页都使用压缩版，提升网页性能的原因。&lt;/p&gt;

&lt;p&gt;下载好后放到网页文件夹中，然后使用 &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;script&amp;gt;&lt;/code&gt; 标签引用，例如：&lt;/p&gt;

&lt;div class=&quot;language-html highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;script &lt;/span&gt;&lt;span class=&quot;na&quot;&gt;scr=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;/js/jquery-3.3.1.min.js&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;路径中填写 &lt;code class=&quot;highlighter-rouge&quot;&gt;.js&lt;/code&gt; 文件的实际存放位置。&lt;/p&gt;

&lt;h5 id=&quot;cdn&quot;&gt;CDN&lt;/h5&gt;

&lt;p&gt;使用CDN（内容分发网络）就可以不用下载jQuery文件，优点是可以使用这个机制尽量避开网络中一些影响数据传输的路线，提高访问速度和稳定性。原理就是使用在各处配置的&lt;strong&gt;节点服务器&lt;/strong&gt;，让用户就近获取所需内容。&lt;/p&gt;

&lt;p&gt;常见CDN有很多，例如百度、新浪、谷歌、微软等，如果是国内站点的话，建议使用国内CDN，国外站点可以使用谷歌或微软，提高速度。&lt;/p&gt;

&lt;p&gt;以百度CDN为例，安装方法如下：&lt;/p&gt;

&lt;div class=&quot;language-html highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;script &lt;/span&gt;&lt;span class=&quot;na&quot;&gt;src=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;微软CDN：&lt;/p&gt;

&lt;div class=&quot;language-html highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;script &lt;/span&gt;&lt;span class=&quot;na&quot;&gt;src=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;http://ajax.microsoft.com/ajax/jquery/jquery-1.3.2.min.js&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;微软jQuery历史版本可以从这里查看&lt;a href=&quot;https://docs.microsoft.com/en-us/aspnet/ajax/cdn/overview#jQuery_Releases_on_the_CDN_0&quot;&gt;https://docs.microsoft.com/en-us/aspnet/ajax/cdn/overview#jQuery_Releases_on_the_CDN_0&lt;/a&gt;。&lt;/p&gt;

&lt;p&gt;更多内容可以访问&lt;strong&gt;百度静态资源公共库&lt;/strong&gt;&lt;a href=&quot;http://cdn.code.baidu.com/&quot;&gt;(http://cdn.code.baidu.com/)&lt;/a&gt;，其他CDN可以自行百度搜索。&lt;/p&gt;

&lt;h4 id=&quot;语法&quot;&gt;语法&lt;/h4&gt;

&lt;p&gt;jQuery的基础语法是通过&lt;strong&gt;选取（query）&lt;/strong&gt;文档中的元素，对其进行&lt;strong&gt;操作（action）&lt;/strong&gt;，语法是：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;$(selector).action()&lt;/code&gt;&lt;/p&gt;

&lt;h5 id=&quot;选择器&quot;&gt;选择器&lt;/h5&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;selector&lt;/code&gt; 是选择器，类似于&lt;strong&gt;CSS选择器&lt;/strong&gt;，常见的有：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;元素选择器，如： &lt;code class=&quot;highlighter-rouge&quot;&gt;$(&quot;p&quot;)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;ID选择器，如：&lt;code class=&quot;highlighter-rouge&quot;&gt;$(&quot;#myId&quot;)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;类选择器，如：&lt;code class=&quot;highlighter-rouge&quot;&gt;$(&quot;.myClass&quot;)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;属性选择器，如：&lt;code class=&quot;highlighter-rouge&quot;&gt;$(&quot;[href]&quot;)&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h5 id=&quot;操作&quot;&gt;操作&lt;/h5&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;action()&lt;/code&gt; 是对所选元素执行的操作，例如：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;隐藏元素：&lt;code class=&quot;highlighter-rouge&quot;&gt;.hide()&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;单击事件：&lt;code class=&quot;highlighter-rouge&quot;&gt;.click(myFunction())&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;双击：&lt;code class=&quot;highlighter-rouge&quot;&gt;.dblclick()&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;悬停：&lt;code class=&quot;highlighter-rouge&quot;&gt;.hover()&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;其他语法与JavaScript类似，代码写在 &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;script&amp;gt;&lt;/code&gt; 中，例如隐藏 p 元素：&lt;/p&gt;

&lt;div class=&quot;language-html highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;script&amp;gt;&lt;/span&gt;
	&lt;span class=&quot;nx&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;p&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;hide&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;有时需要等文档加载完毕后执行代码，很像JavaScript中的 &lt;code class=&quot;highlighter-rouge&quot;&gt;window.onload&lt;/code&gt;：&lt;/p&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;window&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;onload&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(){&lt;/span&gt;
	&lt;span class=&quot;nx&quot;&gt;alert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;jQuery中就要这么写：&lt;/p&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nx&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;document&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;ready&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(){&lt;/span&gt;
	&lt;span class=&quot;nx&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;p&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;hide&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;简化写法：&lt;/p&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nx&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(){&lt;/span&gt;
	&lt;span class=&quot;nx&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;p&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;hide&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;结果都是在整个文档加载完后执行语句。&lt;/p&gt;

&lt;h5 id=&quot;方法链接&quot;&gt;方法链接&lt;/h5&gt;

&lt;p&gt;相对于JavaScript，jQuery又一种特殊的操作方法叫做&lt;strong&gt;方法链接（chaining）&lt;/strong&gt;，即在一条语句上，对一个元素执行多个操作，语法是：
&lt;code class=&quot;highlighter-rouge&quot;&gt;$(selector).action1().action2().action3()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;例如：&lt;/p&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nx&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;p&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;html&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&amp;lt;b&amp;gt;Hello&amp;lt;/b&amp;gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;click&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(){&lt;/span&gt;
	&lt;span class=&quot;nx&quot;&gt;alert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;Hello world&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;结果就是改变元素文本内容后绑定点击事件的调用函数，操作可以绑定多个，并且是&lt;strong&gt;依次执行&lt;/strong&gt;，方法类似，其他操作以此类推。&lt;/p&gt;

            </description>
            <pubDate>Fri, 01 Jun 2018 00:00:00 +0000</pubDate>
            <guid>
                https://knightyun.github.io/2018/06/01/jq-syntax
            </guid>
        </item>
        
        <item>
            <title>addEventListener方法与on事件的区别</title>
            <link>
                https://knightyun.github.io/2018/05/31/js-eventlistener
            </link>
            <description>
                &lt;h4 id=&quot;on事件&quot;&gt;on事件&lt;/h4&gt;

&lt;p&gt;Javascript中可以对一些页面的事件设定触发值，例如常用的点击 &lt;code class=&quot;highlighter-rouge&quot;&gt;onclick&lt;/code&gt;，鼠标移动 &lt;code class=&quot;highlighter-rouge&quot;&gt;onmousemove&lt;/code&gt;，或者移动端屏幕点击 &lt;code class=&quot;highlighter-rouge&quot;&gt;ontouchstart&lt;/code&gt;，其它类似的还有 &lt;code class=&quot;highlighter-rouge&quot;&gt;onmousedown&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;onmouseup&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;onchange&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;onfocus&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;onmouseenter&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;ontouchmove&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;ontouchend&lt;/code&gt; 等等，可以对其设定值来实现事件触发后执行的操作，例如：&lt;/p&gt;

&lt;div class=&quot;language-html highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;h1&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;onclick=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;alert(&#39;hello&#39;);&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;Click me&lt;span class=&quot;nt&quot;&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;点击后就会弹出提示框，也可以这样写：&lt;/p&gt;

&lt;div class=&quot;language-html highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;h1&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;onclick=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;(function(){alert(&quot;hello&quot;);})()&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;Click me&lt;span class=&quot;nt&quot;&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;这样也能实现同样效果，只是它的值变成了一个匿名函数。&lt;/p&gt;

&lt;h4 id=&quot;addeventlistener方法&quot;&gt;addEventListener()方法&lt;/h4&gt;

&lt;p&gt;这个方法设定一个事件监听器，当某一事件发生通过设定的参数执行操作。语法是：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;addEventListener(event, function, useCapture)&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;参数 &lt;code class=&quot;highlighter-rouge&quot;&gt;event&lt;/code&gt; 是必须的，表示监听的事件，例如 &lt;code class=&quot;highlighter-rouge&quot;&gt;click&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;touchstart&lt;/code&gt; 等，就是之前&lt;strong&gt;不加前缀&lt;/strong&gt; &lt;code class=&quot;highlighter-rouge&quot;&gt;on&lt;/code&gt; 的事件。&lt;/li&gt;
  &lt;li&gt;参数 &lt;code class=&quot;highlighter-rouge&quot;&gt;function&lt;/code&gt; 也是必须的，表示事件触发后调用的函数，可以是外部定义函数，也可以是匿名函数。&lt;/li&gt;
  &lt;li&gt;参数 &lt;code class=&quot;highlighter-rouge&quot;&gt;useCapture&lt;/code&gt; 是选填的，填&lt;code class=&quot;highlighter-rouge&quot;&gt;true&lt;/code&gt;或者&lt;code class=&quot;highlighter-rouge&quot;&gt;false&lt;/code&gt;，用于描述事件是&lt;strong&gt;冒泡&lt;/strong&gt;还是**捕获，&lt;code class=&quot;highlighter-rouge&quot;&gt;true&lt;/code&gt;表示捕获，默认的&lt;code class=&quot;highlighter-rouge&quot;&gt;false&lt;/code&gt;表示冒泡。&lt;/li&gt;
&lt;/ul&gt;

&lt;h5 id=&quot;移除事件监听&quot;&gt;移除事件监听&lt;/h5&gt;

&lt;p&gt;如果要移除 addEventListener() 添加的事件监听，就要使用&lt;strong&gt;removeEventListener()&lt;/strong&gt;，语法是：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;removeEventListener(event, function)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;参数与addEventListener()一致。&lt;/p&gt;

&lt;h5 id=&quot;兼容性&quot;&gt;兼容性&lt;/h5&gt;

&lt;p&gt;&lt;strong&gt;IE 8&lt;/strong&gt;及更早的版本，和&lt;strong&gt;Opera 7.0&lt;/strong&gt;及更早的版本，不支持 addEventListener() 和 removeEventListener() 方法，他们使用的是一下方法代替：&lt;/p&gt;

&lt;p&gt;添加事件：
&lt;code class=&quot;highlighter-rouge&quot;&gt;attachEvent(event, function)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;移除事件：
&lt;code class=&quot;highlighter-rouge&quot;&gt;detachEvent(event, function)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;可以用以下方法解决兼容性问题：&lt;/p&gt;

&lt;div class=&quot;language-html highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;div&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;id=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;div1&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;Click me&lt;span class=&quot;nt&quot;&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;script&amp;gt;&lt;/span&gt;
	&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;div1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;document&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;getElementById&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;div1&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;div1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;addEventListener&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;nx&quot;&gt;div1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;addEventListener&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&#39;click&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(){&lt;/span&gt;
			&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;innerHTML&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;your browser is compatible with addEventListener!&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;div1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;attachEvent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;nx&quot;&gt;div1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;attachEvent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&#39;click&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(){&lt;/span&gt;
			&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;innerHTML&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;your browser is not compatible with addEventListener!&quot;&lt;/span&gt;
		&lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h5 id=&quot;冒泡与捕获&quot;&gt;冒泡与捕获&lt;/h5&gt;

&lt;p&gt;这个参数设置的是元素事件的&lt;strong&gt;触发顺序&lt;/strong&gt;，即页面中某元素设置了事件监听，其内部元素也设置有事件监听，&lt;strong&gt;冒泡&lt;/strong&gt;是先触发最内部元素的事件，再依次触发外一层元素的事件，&lt;strong&gt;捕获&lt;/strong&gt;刚好相反，由外到内依次触发。&lt;/p&gt;

&lt;p&gt;综合举例：&lt;/p&gt;

&lt;div class=&quot;language-html highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;div&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;id=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;parent1&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
	&lt;span class=&quot;nt&quot;&gt;&amp;lt;div&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;id=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;child1&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;style=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;height:200px;background:#0cc&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
	冒泡
	&lt;span class=&quot;nt&quot;&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;div&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;id=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;parent2&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
	&lt;span class=&quot;nt&quot;&gt;&amp;lt;div&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;id=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;child2&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;style=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;height:200px;background:#0cc&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
	捕获
	&lt;span class=&quot;nt&quot;&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;

&lt;span class=&quot;nt&quot;&gt;&amp;lt;script&amp;gt;&lt;/span&gt;
	&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;parent1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;document&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;getElementById&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;parent1&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;child2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;document&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;getElementById&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;child1&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;parent2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;document&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;getElementById&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;parent2&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;child2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;document&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;getElementById&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;child2&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;nx&quot;&gt;parent1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;addEventListener&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&#39;click&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(){&lt;/span&gt;
		&lt;span class=&quot;nx&quot;&gt;alert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;this is parent element&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
	&lt;span class=&quot;nx&quot;&gt;child1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;addEventListener&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&#39;click&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(){&lt;/span&gt;
		&lt;span class=&quot;nx&quot;&gt;alert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;this is child element&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
	&lt;span class=&quot;nx&quot;&gt;parent2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;addEventListener&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&#39;click&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(){&lt;/span&gt;
		&lt;span class=&quot;nx&quot;&gt;alert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;this is parent element&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;nx&quot;&gt;child2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;addEventListener&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&#39;click&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(){&lt;/span&gt;
		&lt;span class=&quot;nx&quot;&gt;alert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;this is child element&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;点击“冒泡”模块，先提示子元素后提示父元素；点击“捕获”模块，由于同时设置为 &lt;code class=&quot;highlighter-rouge&quot;&gt;true&lt;/code&gt;，先提示父元素后提示子元素。&lt;/p&gt;

&lt;h4 id=&quot;区别&quot;&gt;区别&lt;/h4&gt;

&lt;p&gt;为某元素设定事件触发函数时，可能会觉得addEventListener和on事件的功能差不多，但是，addEventListener除了可以设置元素&lt;strong&gt;触发顺序&lt;/strong&gt;外，还能&lt;strong&gt;多次绑定事件&lt;/strong&gt;，因为 on 事件多次绑定的话会出现&lt;strong&gt;覆盖&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;举例说明：&lt;/p&gt;

&lt;div class=&quot;language-html highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;div&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;id=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;div1&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;style=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;height:200px;background:#0cc&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
Click me
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;

&lt;span class=&quot;nt&quot;&gt;&amp;lt;script&amp;gt;&lt;/span&gt;
	&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;dib1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;document&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;getElementById&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;div1&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;nx&quot;&gt;div1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;addEventListener&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&#39;click&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(){&lt;/span&gt;
		&lt;span class=&quot;nx&quot;&gt;alert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;message1&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
	&lt;span class=&quot;nx&quot;&gt;div1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;addEventListener&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&#39;click&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(){&lt;/span&gt;
		&lt;span class=&quot;nx&quot;&gt;alert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;message2&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;结果会依次提示“message1”，“message2“。&lt;/p&gt;

&lt;p&gt;但是js这么写的话：&lt;/p&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nx&quot;&gt;div1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;onclick&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(){&lt;/span&gt;
	&lt;span class=&quot;nx&quot;&gt;alert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;message1&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;div1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;onclick&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(){&lt;/span&gt;
	&lt;span class=&quot;nx&quot;&gt;alert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;message2&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;这里就只会提示最后一个“message2”，因为onclick作为对象div1的一个属性，第二次对其进行赋值就会&lt;strong&gt;覆盖&lt;/strong&gt;之前的函数值，这样on事件在某些场合就不适用了。&lt;/p&gt;

            </description>
            <pubDate>Thu, 31 May 2018 00:00:00 +0000</pubDate>
            <guid>
                https://knightyun.github.io/2018/05/31/js-eventlistener
            </guid>
        </item>
        
        <item>
            <title>JavaScript闭包详解</title>
            <link>
                https://knightyun.github.io/2018/05/31/js-closure
            </link>
            <description>
                &lt;h4 id=&quot;变量作用域&quot;&gt;变量作用域&lt;/h4&gt;

&lt;p&gt;首先来了解一下Javascript中变量的作用域，除了常见的普通变量外，&lt;strong&gt;对象&lt;/strong&gt;和&lt;strong&gt;函数&lt;/strong&gt;也是一种变量。变量分为局部变量和全局变量。&lt;/p&gt;

&lt;h5 id=&quot;局部变量&quot;&gt;局部变量&lt;/h5&gt;

&lt;p&gt;局部变量就是指在&lt;strong&gt;函数内部&lt;/strong&gt;定义的变量，作用域是函数内部网，此变量通常只能在函数内部访问，和外界是区分开的，所以变量名即使和外部的&lt;strong&gt;重复&lt;/strong&gt;，也是两个独立的变量，不会相互影响。局部变量在函数执行是创建，执行完后&lt;strong&gt;销毁&lt;/strong&gt;。&lt;/p&gt;

&lt;h5 id=&quot;全局变量&quot;&gt;全局变量&lt;/h5&gt;

&lt;p&gt;全局变量就是函数外部定义的变量，作用域是网页中的所有脚本和函数，它们都能够访问，全局变量是页面加载时创建，页面关闭后销毁。&lt;/p&gt;

&lt;p&gt;综合举例：&lt;/p&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;fun1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;nx&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;这里的 &lt;code class=&quot;highlighter-rouge&quot;&gt;var a = 0;&lt;/code&gt; 就是全局变量，&lt;code class=&quot;highlighter-rouge&quot;&gt;var a = 1;&lt;/code&gt; 是局部变量，虽然名字重复，但这里是两个独立变量，但是还是不建议出现重复，提高代码可读性；&lt;code class=&quot;highlighter-rouge&quot;&gt;b = 2;&lt;/code&gt; 也是&lt;strong&gt;全局变量&lt;/strong&gt;，因为规定函数内部申明的变量，如果不加 &lt;code class=&quot;highlighter-rouge&quot;&gt;var&lt;/code&gt;，即会被认为是全局变量，尤其这点需要小心。&lt;/p&gt;

&lt;h4 id=&quot;闭包&quot;&gt;闭包&lt;/h4&gt;

&lt;p&gt;先通俗的总结一下，闭包就是一个可以访问其他函数内部变量的&lt;strong&gt;函数&lt;/strong&gt;，即一个定义在函数内部的函数，也叫&lt;strong&gt;内嵌函数&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;其次，是闭包的作用，因为通常情况函数内部变量是无法在外部访问的，即全局变量也局部变量的区别，而闭包，就实现了能在外部访问某函数内部变量的功能，让这些变量值始终保存在&lt;strong&gt;内存&lt;/strong&gt;中。&lt;/p&gt;

&lt;p&gt;然后，来讲一下如何实现闭包。有以下代码：&lt;/p&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;fun1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;           &lt;span class=&quot;c1&quot;&gt;//定义一个局部变量&lt;/span&gt;
	&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;fun2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;//这里的 fun2() 就是闭包&lt;/span&gt;
		&lt;span class=&quot;nx&quot;&gt;alert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;//fun2() 是 fun1()的子函数，所以能访问之前定义的局部变量，这个是关键&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;fun2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;         &lt;span class=&quot;c1&quot;&gt;//然后通过这里，把之前得到的局部变量成功返回到外部去&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;fun1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;                      &lt;span class=&quot;c1&quot;&gt;//正常执行函数&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;fun1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;         &lt;span class=&quot;c1&quot;&gt;//将内部变量传递出去，传给变量 result&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;                    &lt;span class=&quot;c1&quot;&gt;//执行这个函数实现对局部变量的访问&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;当然形式不止这一种，万变不离其宗，最后实现的功能是一样的，例如下面的方法也是可行的：&lt;/p&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;fun1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(){&lt;/span&gt;
		&lt;span class=&quot;nx&quot;&gt;alert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;fun1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;fun1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;注意&quot;&gt;注意&lt;/h4&gt;

&lt;p&gt;也许在很多文章中都能看到这句话“避免滥用闭包”，的确，由于闭包会使一些变量一直保存在内存中，所以如果大量使用的话就会消耗大量内存，影响网页性能。&lt;/p&gt;

&lt;p&gt;同时，由于闭包的特性，还会在外部改变函数的内部变量值，有时候这是很危险的，举个例子：&lt;/p&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;fun1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;fun2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;nx&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;nx&quot;&gt;alert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;fun2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;fun1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;                   &lt;span class=&quot;c1&quot;&gt;//函数正常执行，输出 1&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;change&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;fun1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;change&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;                 &lt;span class=&quot;c1&quot;&gt;//在外部执行这个函数后，函数内部变量 a 的值就被改变了，输出 2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

            </description>
            <pubDate>Thu, 31 May 2018 00:00:00 +0000</pubDate>
            <guid>
                https://knightyun.github.io/2018/05/31/js-closure
            </guid>
        </item>
        
        <item>
            <title>JavaScript计时器函数用法</title>
            <link>
                https://knightyun.github.io/2018/05/24/js-timer-function
            </link>
            <description>
                &lt;p&gt;Javascript中和大多数语言一样，存在计时函数，使某语句或函数不用立即执行，可以延时设定的时间值之后再执行。&lt;/p&gt;

&lt;h4 id=&quot;settimeout-方法&quot;&gt;setTimeout() 方法&lt;/h4&gt;

&lt;p&gt;这个函数表示括号中的代码，延时指定时间后再执行，格式为 &lt;code class=&quot;highlighter-rouge&quot;&gt;setTimeout(&quot;function()&quot;, time)&lt;/code&gt;，其中 &lt;code class=&quot;highlighter-rouge&quot;&gt;time&lt;/code&gt; 的单位是&lt;strong&gt;毫秒&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;例如：&lt;/p&gt;

&lt;div class=&quot;language-javascript highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;fx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;nx&quot;&gt;alert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;setTimeout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;fx()&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;也可以写成：&lt;/p&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nx&quot;&gt;setTimoeout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(){&lt;/span&gt;
	&lt;span class=&quot;nx&quot;&gt;alert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;结果就是页面加载完 2 秒后弹出提示框。&lt;/p&gt;

&lt;h4 id=&quot;cleartimeout-方法&quot;&gt;clearTimeout() 方法&lt;/h4&gt;

&lt;p&gt;clearTimeout() 方法用于结束 setTimeout() 方法的执行，括号中&lt;strong&gt;参数&lt;/strong&gt;为 setTimeout() 返回的 &lt;strong&gt;ID 值&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;举例说明：&lt;/p&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;int1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;setTimeout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(){&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;alert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();},&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;clearTimeout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;int1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;这样就能&lt;strong&gt;终止&lt;/strong&gt;代码执行，不会弹出提示框。&lt;/p&gt;

&lt;h4 id=&quot;setinterval-方法&quot;&gt;setInterval() 方法&lt;/h4&gt;

&lt;p&gt;这个函数表示&lt;strong&gt;每隔&lt;/strong&gt;指定时间间隔执行一次括号中的代码，格式为：&lt;code class=&quot;highlighter-rouge&quot;&gt;setInterval(&quot;function()&quot;, time)&lt;/code&gt;， &lt;code class=&quot;highlighter-rouge&quot;&gt;time&lt;/code&gt; 单位依然为毫秒。&lt;/p&gt;

&lt;p&gt;例如：&lt;/p&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;fx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;nb&quot;&gt;document&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;0&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;setInterval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;fx()&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;这里就不要用 &lt;code class=&quot;highlighter-rouge&quot;&gt;alert()&lt;/code&gt; 做实验了，后果你懂的 -_- .&lt;/p&gt;

&lt;p&gt;同样也能写成：&lt;/p&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nx&quot;&gt;setInterval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(){&lt;/span&gt;
	&lt;span class=&quot;nb&quot;&gt;document&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;0&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;效果就是不断输出字符“0”。&lt;/p&gt;

&lt;h4 id=&quot;clearinterval-方法&quot;&gt;clearInterval() 方法&lt;/h4&gt;

&lt;p&gt;用法与 clearTimeout() 一样，终止 setInterval() 的执行，括号中填 setInterval() 的返回值。&lt;/p&gt;

&lt;p&gt;例如：&lt;/p&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;int2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;setInterval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()[&lt;/span&gt;
	&lt;span class=&quot;nb&quot;&gt;document&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;0&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;clearInterval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;int2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;这样就能终止输出。&lt;/p&gt;

&lt;h4 id=&quot;注意&quot;&gt;注意&lt;/h4&gt;

&lt;p&gt;有个小问题，用 setTimeout() 举例，假如代码像下面这样写：&lt;/p&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;fx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;nx&quot;&gt;alert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;setTimeout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;fx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;相比上面，就是函数第一个参数少了双引号，猜一下后果会怎样……&lt;/p&gt;

&lt;p&gt;后果当然是页面加载后立刻弹出提示框，并不会延时 3 秒。下面的写法也是类似的效果：&lt;/p&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nx&quot;&gt;setTimeout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(){&lt;/span&gt;
	&lt;span class=&quot;nx&quot;&gt;alert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;})(),&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;原因都一样，无论是语句块 &lt;code class=&quot;highlighter-rouge&quot;&gt;fx()&lt;/code&gt; 还是匿名函数 &lt;code class=&quot;highlighter-rouge&quot;&gt;(function(){})()&lt;/code&gt;，都是会&lt;strong&gt;立刻执行&lt;/strong&gt;的语句，而加双引号的 &lt;code class=&quot;highlighter-rouge&quot;&gt;&quot;fx()&quot;&lt;/code&gt; 和 &lt;code class=&quot;highlighter-rouge&quot;&gt;function(){}&lt;/code&gt; 就是当成一个&lt;strong&gt;参数&lt;/strong&gt;传递给了函数 setTimeout()，然后这个&lt;strong&gt;参数&lt;/strong&gt;语句直到 setTimeou() 真正执行时才生效，也就是延时3秒后执行。&lt;/p&gt;

&lt;p&gt;函数 setInterval() 的这个性质与 setTimeout() &lt;strong&gt;类似&lt;/strong&gt;。&lt;/p&gt;

            </description>
            <pubDate>Thu, 24 May 2018 00:00:00 +0000</pubDate>
            <guid>
                https://knightyun.github.io/2018/05/24/js-timer-function
            </guid>
        </item>
        
        <item>
            <title>JavaScript中语句与函数的执行辨析</title>
            <link>
                https://knightyun.github.io/2018/05/23/js-anonymous-function
            </link>
            <description>
                &lt;p&gt;Javascript代码中，语句和函数以及匿名函数的执行存在一些区别，所以在编写过程中也存在一些“坑“。&lt;/p&gt;

&lt;h4 id=&quot;script-中的语句&quot;&gt;script 中的语句&lt;/h4&gt;

&lt;p&gt;html文档中的javascript语句是写在 &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;script&amp;gt;&amp;lt;/script&amp;gt;&lt;/code&gt; 中的，每条语句末尾需要添加分号 &lt;code class=&quot;highlighter-rouge&quot;&gt;;&lt;/code&gt;，表示此条语句执行结束。例如下面的代码：&lt;/p&gt;

&lt;div class=&quot;language-html highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;script&amp;gt;&lt;/span&gt;
	&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;nx&quot;&gt;alert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;文档加载到这块代码区域时，就会立刻&lt;strong&gt;执行&lt;/strong&gt;这两条语句，即弹出提示框为x的值，但是如果把语句换成函数定义，代码如下：&lt;/p&gt;

&lt;div class=&quot;language-html highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;script&amp;gt;&lt;/span&gt;
	&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;fx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;nx&quot;&gt;alert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;nx&quot;&gt;fx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;这时第一行语句会被&lt;strong&gt;执行&lt;/strong&gt;，第二至五行是&lt;strong&gt;函数定义&lt;/strong&gt;，并不会执行这个函数，直到最后一行语句才会真正&lt;strong&gt;执行&lt;/strong&gt;这个定义过的函数。&lt;/p&gt;

&lt;p&gt;如果需要立刻执行函数的话，就需要使用&lt;strong&gt;匿名函数&lt;/strong&gt;了。所谓匿名函数，顾名思义，即不会给这个执行的函数定义“函数名”，而且是一个立即执行的语句。格式如下：&lt;/p&gt;

&lt;div class=&quot;language-html highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;script&amp;gt;&lt;/span&gt;
	&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(){&lt;/span&gt;
		&lt;span class=&quot;nx&quot;&gt;alert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;})();&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;注意&lt;/strong&gt;代码中的三个&lt;strong&gt;括号&lt;/strong&gt;的位置，以及最后跟的那个&lt;strong&gt;分号&lt;/strong&gt;，表示这是一个立即执行的语句。&lt;/p&gt;

&lt;p&gt;当然匿名函数也能&lt;strong&gt;传递参数&lt;/strong&gt;，例如：&lt;/p&gt;

&lt;div class=&quot;language-html highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;script&amp;gt;&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;){&lt;/span&gt;
		&lt;span class=&quot;nx&quot;&gt;alert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;})(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;效果和上面一样。&lt;/p&gt;

&lt;p&gt;但有时又需要不立即执行的函数，例如：&lt;/p&gt;

&lt;div class=&quot;language-html highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;script&amp;gt;&lt;/span&gt;
	&lt;span class=&quot;nx&quot;&gt;setTimeout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(){&lt;/span&gt;
		&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;nx&quot;&gt;alert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;效果与下面代码一样：&lt;/p&gt;

&lt;div class=&quot;language-html highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;script&amp;gt;&lt;/span&gt;
	&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;nx&quot;&gt;setTimeout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;alert(x)&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;这是Javascript中的延时函数，表示2秒后弹出提示。&lt;code class=&quot;highlighter-rouge&quot;&gt;setTimeout()&lt;/code&gt; 自身就是一个&lt;strong&gt;函数&lt;/strong&gt;，里面的 &lt;code class=&quot;highlighter-rouge&quot;&gt;“alert(x)”&lt;/code&gt; 是这个函数的一个&lt;strong&gt;参数&lt;/strong&gt;，即一个&lt;strong&gt;加引号的语句&lt;/strong&gt;。便于理解，可以写成这样：&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;setTimeout(&quot;alert();&quot;, 2000);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;这样写并不会出错。&lt;/p&gt;

&lt;p&gt;所以这个函数 &lt;code class=&quot;highlighter-rouge&quot;&gt;setTimeout()&lt;/code&gt; 的参数是一个&lt;strong&gt;不用立即执行&lt;/strong&gt;的匿名函数 &lt;code class=&quot;highlighter-rouge&quot;&gt;function(){}&lt;/code&gt;，也可以是一个语句块，从而进行&lt;strong&gt;参数传递&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;通俗讲，这里加引号的语句块相当于不加引号的匿名函数。&lt;/p&gt;

&lt;h4 id=&quot;标签属性中的语句&quot;&gt;标签属性中的语句&lt;/h4&gt;

&lt;p&gt;在 html 标签中也能使用语句，通常用于设置元素的属性。&lt;/p&gt;

&lt;p&gt;先对比区分以下代码：&lt;/p&gt;

&lt;div class=&quot;language-html highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;script&amp;gt;&lt;/span&gt;
	&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;fx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;nx&quot;&gt;alert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;

&lt;span class=&quot;nt&quot;&gt;&amp;lt;button&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;onclick=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;alert()&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;Button1&lt;span class=&quot;nt&quot;&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;

&lt;span class=&quot;nt&quot;&gt;&amp;lt;button&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;onclick=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;fx()&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;Button2&lt;span class=&quot;nt&quot;&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;

&lt;span class=&quot;nt&quot;&gt;&amp;lt;button&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;onclick=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;fx()&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;Button3&lt;span class=&quot;nt&quot;&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;

&lt;span class=&quot;nt&quot;&gt;&amp;lt;button&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;onclick=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;(function(){alert();})()&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;Button4&lt;span class=&quot;nt&quot;&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;

&lt;span class=&quot;nt&quot;&gt;&amp;lt;button&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;onclick=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;(function(){alert();})()&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;Button5&lt;span class=&quot;nt&quot;&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;

&lt;span class=&quot;nt&quot;&gt;&amp;lt;button&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;onclick=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;function(){alert();}&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;Button6&lt;span class=&quot;nt&quot;&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;

&lt;span class=&quot;nt&quot;&gt;&amp;lt;button&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;onclick=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;function(){alert();}&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;Button7&lt;span class=&quot;nt&quot;&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;猜一下哪个按钮点击无效……&lt;/p&gt;

&lt;p&gt;答案是最后的&lt;strong&gt;“Button6”&lt;/strong&gt;和&lt;strong&gt;“Button7”&lt;/strong&gt;，然后就能发现规律了，&lt;strong&gt;属性&lt;/strong&gt;所设置的&lt;strong&gt;值&lt;/strong&gt;必须是能&lt;strong&gt;立即执行&lt;/strong&gt;的&lt;u&gt;语句块&lt;/u&gt;、&lt;u&gt;函数&lt;/u&gt;或&lt;u&gt;匿名函数&lt;/u&gt;，最后那两种情况是行不通的。&lt;/p&gt;

            </description>
            <pubDate>Wed, 23 May 2018 00:00:00 +0000</pubDate>
            <guid>
                https://knightyun.github.io/2018/05/23/js-anonymous-function
            </guid>
        </item>
        
        <item>
            <title>web浏览器进化简史</title>
            <link>
                https://knightyun.github.io/2018/05/22/web-browser-history
            </link>
            <description>
                &lt;h4 id=&quot;发展史与时间线&quot;&gt;发展史与时间线&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;1989年，科学家&lt;strong&gt;&lt;em&gt;Tim-Berners-Lee&lt;/em&gt;&lt;/strong&gt;发明了&lt;strong&gt;World Wide Web&lt;/strong&gt;（万维网）。
&lt;img src=&quot;https://i.loli.net/2018/05/22/5b04323d35dcc.png&quot; alt=&quot;www.png&quot; /&gt;&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;1990年，他开发了世界上第一款网页浏览器，为避免与万维网混淆，改名为&lt;strong&gt;NEXUS&lt;/strong&gt;，但不支持图片。
&lt;img src=&quot;https://i.loli.net/2018/05/22/5b04323d6cf2f.png&quot; alt=&quot;nexus.png&quot; /&gt;&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;1993年，伊利诺大学的NCSA组织创造了第一款可显示图片的浏览器，&lt;strong&gt;Mosaic&lt;/strong&gt;（马赛克）
&lt;img src=&quot;https://i.loli.net/2018/05/22/5b04323d04dfc.gif&quot; alt=&quot;mosaic.gif&quot; /&gt;&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;1994年10月在麻省理工学院计算机科学实验室成立&lt;strong&gt;万维网联盟&lt;/strong&gt;。建立者正是万维网的发明者&lt;strong&gt;蒂姆·伯纳斯·李&lt;/strong&gt;。万维网联盟(&lt;strong&gt;World Wide Web Consortium&lt;/strong&gt;，简称&lt;strong&gt;W3C&lt;/strong&gt;)。
&lt;img src=&quot;https://i.loli.net/2018/05/22/5b04323c3986d.jpg&quot; alt=&quot;w3c.jpg&quot; /&gt;&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;1994年，&lt;strong&gt;Mozilla&lt;/strong&gt;出现了。不过，鉴于当时 Mosaic 的权势。为了避嫌，最终改名成了&lt;strong&gt;Netscape Navigator&lt;/strong&gt;（网景公司开发的网络浏览器）。凭借着html框架显示等新特性，很快成为了&lt;strong&gt;新的霸主&lt;/strong&gt;。
&lt;img src=&quot;https://i.loli.net/2018/05/22/5b04323c69a6d.jpg&quot; alt=&quot;nn.jpg&quot; /&gt;&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;1995年微软发布了跟系统“捆绑”的浏览器&lt;strong&gt;Internet Explorer（IE）&lt;/strong&gt;，凭借着操作系统的占有率，&lt;strong&gt;IE&lt;/strong&gt;就把&lt;strong&gt;Netscape&lt;/strong&gt;挤下了霸主宝座。
&lt;img src=&quot;https://i.loli.net/2018/05/22/5b04323cc82c2.jpg&quot; alt=&quot;ie.jpg&quot; /&gt;&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;和 &lt;strong&gt;IE&lt;/strong&gt; 差不多时间诞生，还有一直不温不火的 &lt;strong&gt;Opera&lt;/strong&gt;。
&lt;img src=&quot;https://i.loli.net/2018/05/22/5b04326e039df.jpg&quot; alt=&quot;opera.jpg&quot; /&gt;&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;NetScape&lt;/strong&gt;并未放弃，围绕着浏览器引擎衍生出了人们熟知 &lt;strong&gt;Firefox&lt;/strong&gt;。
&lt;img src=&quot;https://i.loli.net/2018/05/22/5b04326e06bfd.jpg&quot; alt=&quot;firefox.jpg&quot; /&gt;&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;2003年，苹果推出的 &lt;strong&gt;Safari&lt;/strong&gt;。
&lt;img src=&quot;https://i.loli.net/2018/05/22/5b04326e023b8.jpg&quot; alt=&quot;safari.jpg&quot; /&gt;&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;2008年 &lt;strong&gt;Google&lt;/strong&gt; 携 &lt;strong&gt;Chrome&lt;/strong&gt; 参战，让 &lt;strong&gt;IE&lt;/strong&gt; 逐渐失利。
&lt;img src=&quot;https://i.loli.net/2018/05/22/5b04326e31caf.jpg&quot; alt=&quot;chrome.jpg&quot; /&gt;&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;2015年，微软为了改变局面，推出了 &lt;strong&gt;Edge&lt;/strong&gt;。
&lt;img src=&quot;https://i.loli.net/2018/05/22/5b04326e00c7f.jpg&quot; alt=&quot;edge.jpg&quot; /&gt;&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;但 &lt;strong&gt;Chrome&lt;/strong&gt; 以及类似内核的浏览器依旧是主流。
&lt;img src=&quot;https://i.loli.net/2018/05/22/5b04326e052f1.jpg&quot; alt=&quot;chrome-inner.jpg&quot; /&gt;&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

            </description>
            <pubDate>Tue, 22 May 2018 00:00:00 +0000</pubDate>
            <guid>
                https://knightyun.github.io/2018/05/22/web-browser-history
            </guid>
        </item>
        
        <item>
            <title>CSS样式之内容居中方法</title>
            <link>
                https://knightyun.github.io/2018/05/22/css-content-center
            </link>
            <description>
                &lt;h3 id=&quot;水平居中&quot;&gt;水平居中&lt;/h3&gt;

&lt;p&gt;HTML中要实现某一内容水平居中显示，要通过设置css样式来实现，主要分为&lt;strong&gt;行内元素&lt;/strong&gt;和&lt;strong&gt;块状元素&lt;/strong&gt;两种情况，&lt;strong&gt;块状元素&lt;/strong&gt;又可分为&lt;strong&gt;块状定宽&lt;/strong&gt;与&lt;strong&gt;块状不定宽&lt;/strong&gt;两种情况，接下来依次介绍分析。&lt;/p&gt;

&lt;h4 id=&quot;行内元素&quot;&gt;行内元素&lt;/h4&gt;

&lt;p&gt;像 a、span、i 这类元素叫做行内元素，&lt;strong&gt;文本&lt;/strong&gt;和&lt;strong&gt;图片&lt;/strong&gt;也是行内元素。行内元素水平居中方法简单，只需要给行内元素的&lt;strong&gt;父元素&lt;/strong&gt;设置 &lt;code class=&quot;highlighter-rouge&quot;&gt;text-align: center;&lt;/code&gt; css样式就内实现内容水平居中，例如：
html:&lt;/p&gt;

&lt;div class=&quot;language-html highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;div&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;class =&lt;/span&gt;&lt;span class=&quot;err&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;txt&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;居中内容&lt;span class=&quot;nt&quot;&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;css:&lt;/p&gt;

&lt;div class=&quot;language-css highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nc&quot;&gt;.txt&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;nl&quot;&gt;text-align&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;center&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;文本内容的&lt;strong&gt;父元素&lt;/strong&gt;就是 &lt;code class=&quot;highlighter-rouge&quot;&gt;div&lt;/code&gt; ，这样就内实现水平居中，效果如下：&lt;/p&gt;

&lt;div align=&quot;center&quot;&gt;&lt;b&gt;居中内容&lt;/b&gt;&lt;/div&gt;

&lt;h4 id=&quot;块状定宽元素&quot;&gt;块状定宽元素&lt;/h4&gt;

&lt;p&gt;常见块状元素有 div, p, h 等，定宽即为其设置固定宽度值 &lt;code class=&quot;highlighter-rouge&quot;&gt;width&lt;/code&gt;，这时我们可以为元素设置 &lt;code class=&quot;highlighter-rouge&quot;&gt;margin-left&lt;/code&gt; 和 &lt;code class=&quot;highlighter-rouge&quot;&gt;margin-right&lt;/code&gt; 来实现水平居中，也可以简写为 &lt;code class=&quot;highlighter-rouge&quot;&gt;margin: 0 auto;&lt;/code&gt;，例如：
html:&lt;/p&gt;

&lt;div class=&quot;language-html highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;div&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;class =&lt;/span&gt;&lt;span class=&quot;err&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;txt&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;居中内容&lt;span class=&quot;nt&quot;&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;css:&lt;/p&gt;

&lt;div class=&quot;language-css highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nc&quot;&gt;.txt&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;nl&quot;&gt;width&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;100px&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;nl&quot;&gt;margin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;10px&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;auto&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;实现效果如下：&lt;/p&gt;

&lt;div align=&quot;center&quot;&gt;&lt;b&gt;居中内容&lt;/b&gt;&lt;/div&gt;

&lt;h4 id=&quot;块状不定宽&quot;&gt;块状不定宽&lt;/h4&gt;

&lt;p&gt;有时候我们不能限制块状元素的宽度，就是块状不定宽元素，主要有三种方法，接下来一次介绍。&lt;/p&gt;

&lt;h5 id=&quot;1加入-table-标签&quot;&gt;1、加入 table 标签&lt;/h5&gt;

&lt;p&gt;利用 table 标签的&lt;strong&gt;长度自适应性&lt;/strong&gt;，长度根据内容自动调整，然后通过设置 &lt;code class=&quot;highlighter-rouge&quot;&gt;margin: auto;&lt;/code&gt; 实现水平居中，例如：
html:&lt;/p&gt;

&lt;div class=&quot;language-html highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;table&amp;gt;&lt;/span&gt;
	&lt;span class=&quot;nt&quot;&gt;&amp;lt;tbody&amp;gt;&lt;/span&gt;
		&lt;span class=&quot;nt&quot;&gt;&amp;lt;tr&amp;gt;&lt;/span&gt;
			&lt;span class=&quot;nt&quot;&gt;&amp;lt;td&amp;gt;&lt;/span&gt;
			&lt;span class=&quot;nt&quot;&gt;&amp;lt;div&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;class =&lt;/span&gt;&lt;span class=&quot;err&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;txt&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;居中内容&lt;span class=&quot;nt&quot;&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
			&lt;span class=&quot;nt&quot;&gt;&amp;lt;/td&amp;gt;&lt;/span&gt;
		&lt;span class=&quot;nt&quot;&gt;&amp;lt;/tr&amp;gt;&lt;/span&gt;
	&lt;span class=&quot;nt&quot;&gt;&amp;lt;/tbody&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/table&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;css:&lt;/p&gt;

&lt;div class=&quot;language-css highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nc&quot;&gt;.txt&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;nl&quot;&gt;margin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;auto&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;效果如下：&lt;/p&gt;

&lt;div align=&quot;center&quot;&gt;&lt;b&gt;居中内容&lt;/b&gt;&lt;/div&gt;

&lt;h5 id=&quot;2设置为行内元素&quot;&gt;2、设置为行内元素&lt;/h5&gt;

&lt;p&gt;就是通过设置 &lt;code class=&quot;highlighter-rouge&quot;&gt;display: inline;&lt;/code&gt; 将块状元素设置为&lt;strong&gt;行内元素&lt;/strong&gt;，然后就是像行内元素一样设置 &lt;code class=&quot;highlighter-rouge&quot;&gt;text-align: center;&lt;/code&gt; 来是内容水平居中，例如：
html:&lt;/p&gt;

&lt;div class=&quot;language-html highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;div&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;class =&lt;/span&gt;&lt;span class=&quot;err&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;txt&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;居中内容&lt;span class=&quot;nt&quot;&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;css:&lt;/p&gt;

&lt;div class=&quot;language-css highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nc&quot;&gt;.txt&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;nl&quot;&gt;display&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;inline&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;nl&quot;&gt;text-align&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;center&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;效果为：&lt;/p&gt;

&lt;div align=&quot;center&quot;&gt;&lt;b&gt;居中内容&lt;/b&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;注：&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;使用这种方法虽然可以不用像table增加无语义标签，但是改变了display，所以会少了一些功能，例如不能设置宽度。&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h5 id=&quot;3设置浮动和相对定位&quot;&gt;3、设置浮动和相对定位&lt;/h5&gt;

&lt;p&gt;这种方法设置就相对复杂，同时设置&lt;strong&gt;浮动&lt;/strong&gt;和&lt;strong&gt;相对定位&lt;/strong&gt;来实现元素的水平居中。
首先设置&lt;strong&gt;父元素&lt;/strong&gt;：&lt;/p&gt;

&lt;div class=&quot;language-css highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;left&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt; 
&lt;span class=&quot;nt&quot;&gt;position&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;relative&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt; 
&lt;span class=&quot;nt&quot;&gt;left&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;%;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后设置&lt;strong&gt;子元素&lt;/strong&gt;：&lt;/p&gt;

&lt;div class=&quot;language-css highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;position&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;relative&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;left&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-50&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;%;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;通过代码应该好理解，就是通过&lt;strong&gt;50%&lt;/strong&gt;那个关键位置来实现水平居中效果，因为&lt;strong&gt;50%&lt;/strong&gt;是界面的中央位置，将父元素右移，直到左边框移到中线位置，在将子元素向左移，这样子元素不就居中了吗。&lt;/p&gt;

&lt;p&gt;这里要&lt;strong&gt;注意&lt;/strong&gt;的是分别设置&lt;strong&gt;父元素&lt;/strong&gt;的 &lt;code class=&quot;highlighter-rouge&quot;&gt;50%&lt;/code&gt; 和&lt;strong&gt;子元素&lt;/strong&gt;的 &lt;code class=&quot;highlighter-rouge&quot;&gt;-50%&lt;/code&gt;。
实现效果如下：&lt;/p&gt;

&lt;div align=&quot;center&quot;&gt;&lt;b&gt;居中内容&lt;/b&gt;&lt;/div&gt;

&lt;h3 id=&quot;垂直居中&quot;&gt;垂直居中&lt;/h3&gt;

&lt;p&gt;说完水平居中接着说垂直居中，这里主要又分为两种情况：&lt;strong&gt;父元素高度确定的单行文本&lt;/strong&gt; 和&lt;strong&gt;父元素高度确定的多行文本&lt;/strong&gt;。&lt;/p&gt;

&lt;h4 id=&quot;单行文本&quot;&gt;单行文本&lt;/h4&gt;

&lt;p&gt;对于&lt;strong&gt;父元素&lt;/strong&gt;高度确定的单行文本，可以通过设置&lt;strong&gt;父元素&lt;/strong&gt;的 &lt;code class=&quot;highlighter-rouge&quot;&gt;height&lt;/code&gt; 和 &lt;code class=&quot;highlighter-rouge&quot;&gt;line-height&lt;/code&gt; 高度一致来实现。&lt;/p&gt;

&lt;p&gt;这里可以这样理解，&lt;code class=&quot;highlighter-rouge&quot;&gt;height&lt;/code&gt; 是元素的高度，例如文本字体的高度，&lt;code class=&quot;highlighter-rouge&quot;&gt;line-height&lt;/code&gt; 是行高，例如文本的行间距，一行文本中，行间距被&lt;strong&gt;分为两部分&lt;/strong&gt;，分别位于这行文本的&lt;strong&gt;顶部&lt;/strong&gt;和&lt;strong&gt;底部&lt;/strong&gt;（因为行间距是两行之间的距离），所以设置 &lt;code class=&quot;highlighter-rouge&quot;&gt;height&lt;/code&gt; 和 &lt;code class=&quot;highlighter-rouge&quot;&gt;line-height&lt;/code&gt; &lt;strong&gt;一样大&lt;/strong&gt;的话，&lt;code class=&quot;highlighter-rouge&quot;&gt;line-height&lt;/code&gt; 就被均分为两部分，分别位于元素顶部和底部，这样中间设置为 &lt;code class=&quot;highlighter-rouge&quot;&gt;height&lt;/code&gt; 的元素不就实现&lt;strong&gt;垂直居中&lt;/strong&gt;了吗 ^_^ .&lt;/p&gt;

&lt;p&gt;例如：
html:&lt;/p&gt;

&lt;div class=&quot;language-html highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;div&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;class =&lt;/span&gt;&lt;span class=&quot;err&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;txt&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;居中内容&lt;span class=&quot;nt&quot;&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;css:&lt;/p&gt;

&lt;div class=&quot;language-css highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nc&quot;&gt;.txt&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;nl&quot;&gt;height&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;200px&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;nl&quot;&gt;line-height&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;200px&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;效果如下：&lt;/p&gt;

&lt;pre&gt;



居中内容



&lt;/pre&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;这里需要注意的是关键词“单行文本”，如果使用这种方法但是一行文本超过宽度限制的话，某些内容就会脱离元素块，子元素有多行的话，这几行就会并排居中，并保持设置的行高。&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4 id=&quot;多行文本&quot;&gt;多行文本&lt;/h4&gt;

&lt;p&gt;对于父元素高度确定的&lt;strong&gt;文本&lt;/strong&gt;和&lt;strong&gt;图片&lt;/strong&gt;等内容设置垂直居中，主要有两种方法。&lt;/p&gt;

&lt;h5 id=&quot;1使用-table-标签&quot;&gt;1、使用 table 标签&lt;/h5&gt;

&lt;p&gt;对元素使用&lt;strong&gt;table&lt;/strong&gt;标签，包括 tbody，tr，td，然后对父元素设置 &lt;code class=&quot;highlighter-rouge&quot;&gt;vertical-align: middle;&lt;/code&gt; 样式，就能使 &lt;code class=&quot;highlighter-rouge&quot;&gt;inline-block&lt;/code&gt; 类型的子元素垂直居中显示。&lt;/p&gt;

&lt;p&gt;因为 td 标签&lt;strong&gt;默认&lt;/strong&gt;设置了 &lt;code class=&quot;highlighter-rouge&quot;&gt;vertical-align: middle&lt;/code&gt;，所以也可以不用单独设置 &lt;code class=&quot;highlighter-rouge&quot;&gt;vertical-align&lt;/code&gt;。
例如：
html:&lt;/p&gt;

&lt;div class=&quot;language-html highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;table&amp;gt;&lt;/span&gt;
	&lt;span class=&quot;nt&quot;&gt;&amp;lt;tbody&amp;gt;&lt;/span&gt;
		&lt;span class=&quot;nt&quot;&gt;&amp;lt;tr&amp;gt;&lt;/span&gt;
			&lt;span class=&quot;nt&quot;&gt;&amp;lt;td&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;class=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;txt&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
				&lt;span class=&quot;nt&quot;&gt;&amp;lt;p&amp;gt;&lt;/span&gt;居中内容&lt;span class=&quot;nt&quot;&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
				&lt;span class=&quot;nt&quot;&gt;&amp;lt;p&amp;gt;&lt;/span&gt;居中内容&lt;span class=&quot;nt&quot;&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
				&lt;span class=&quot;nt&quot;&gt;&amp;lt;p&amp;gt;&lt;/span&gt;居中内容&lt;span class=&quot;nt&quot;&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
			&lt;span class=&quot;nt&quot;&gt;&amp;lt;/td&amp;gt;&lt;/span&gt;
		&lt;span class=&quot;nt&quot;&gt;&amp;lt;/tr&amp;gt;&lt;/span&gt;
	&lt;span class=&quot;nt&quot;&gt;&amp;lt;/tbody&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/table&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;css:&lt;/p&gt;

&lt;div class=&quot;language-css highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nc&quot;&gt;.txt&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nl&quot;&gt;height&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;300px&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;nl&quot;&gt;background-color&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;#ccc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;这里的父元素就是 td，父元素的高度必须确定，就要为其设置 height。&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;注意&lt;/strong&gt;这里的 p 元素是 &lt;strong&gt;inline&lt;/strong&gt; 类型的，所以设置 &lt;code class=&quot;highlighter-rouge&quot;&gt;vertical-align: middle&lt;/code&gt; 的话会出现错误，若果是图片元素 img 的话，就可以设置 &lt;code class=&quot;highlighter-rouge&quot;&gt;vertical-align: middle&lt;/code&gt;，但是由于 td 标签默认，所以都可以不写。&lt;/p&gt;

&lt;p&gt;效果如下：&lt;/p&gt;

&lt;pre&gt;



居中内容



&lt;/pre&gt;

&lt;h5 id=&quot;2设置-table-cell&quot;&gt;2、设置 table-cell&lt;/h5&gt;

&lt;p&gt;第二种方法是把要垂直居中显示的元素的父元素设置为&lt;strong&gt;table-cell (表格单元)&lt;/strong&gt;类型：&lt;code class=&quot;highlighter-rouge&quot;&gt;display: table-cell;&lt;/code&gt;，然后设置 &lt;code class=&quot;highlighter-rouge&quot;&gt;vertical-align: middle&lt;/code&gt;就能实现元素垂直居中。&lt;/p&gt;

&lt;p&gt;但是这个方法存在兼容性问题，&lt;strong&gt;chrome, firefox, IE8以上&lt;/strong&gt;才支持这个操作。&lt;/p&gt;

&lt;p&gt;例如：
html:&lt;/p&gt;

&lt;div class=&quot;language-html highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;div&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;class=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;txt&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
	&lt;span class=&quot;nt&quot;&gt;&amp;lt;p&amp;gt;&lt;/span&gt;居中内容&lt;span class=&quot;nt&quot;&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
	&lt;span class=&quot;nt&quot;&gt;&amp;lt;p&amp;gt;&lt;/span&gt;居中内容&lt;span class=&quot;nt&quot;&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
	&lt;span class=&quot;nt&quot;&gt;&amp;lt;p&amp;gt;&lt;/span&gt;居中内容&lt;span class=&quot;nt&quot;&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;css:&lt;/p&gt;

&lt;div class=&quot;language-css highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nc&quot;&gt;.txt&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nl&quot;&gt;height&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;300px&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;nl&quot;&gt;background-color&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;#ccc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;nl&quot;&gt;display&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;table-cell&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;nl&quot;&gt;vertical-align&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;middle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;同样，要为父元素 div 设置高度 height&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;效果如下：&lt;/p&gt;

&lt;pre&gt;



居中内容



&lt;/pre&gt;

&lt;p&gt;这种方法除了兼容性问题外，同时也改变了 &lt;strong&gt;display&lt;/strong&gt;类型，会在某些方面带来不便。&lt;/p&gt;

            </description>
            <pubDate>Tue, 22 May 2018 00:00:00 +0000</pubDate>
            <guid>
                https://knightyun.github.io/2018/05/22/css-content-center
            </guid>
        </item>
        
        <item>
            <title>css布局模型详细介绍</title>
            <link>
                https://knightyun.github.io/2018/05/04/css-layout-model
            </link>
            <description>
                &lt;p&gt;HTML中元素有三种布局模型：流动模型、浮动模型、层模型。&lt;/p&gt;

&lt;h4 id=&quot;流动模型flow&quot;&gt;流动模型（flow）&lt;/h4&gt;
&lt;p&gt;HTML网页默认布局就是流动模型，布局如下：&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;块级元素(block)&lt;/strong&gt;自上而下垂直分布，因为块级元素默认宽度为浏览器窗口的&lt;strong&gt;100%&lt;/strong&gt;，或者理解为每个块级元素默认&lt;strong&gt;占一行&lt;/strong&gt;。常见块级元素有 &lt;code class=&quot;highlighter-rouge&quot;&gt;div&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;p&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;h&lt;/code&gt; 等；&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;内联元素(inline)&lt;/strong&gt;从左到右水平分布，即不像块级元素那样每个独占一行。常见内联元素有 &lt;code class=&quot;highlighter-rouge&quot;&gt;a&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;span&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;em&lt;/code&gt; 等。&lt;/p&gt;

&lt;h4 id=&quot;浮动模型float&quot;&gt;浮动模型（float）&lt;/h4&gt;
&lt;p&gt;上面提到的块级元素是每个独占一行显示，但是定义css浮动模型后就能使两个块级元素&lt;strong&gt;并排一行&lt;/strong&gt;显示。
例如HTML代码：&lt;/p&gt;

&lt;div class=&quot;language-html highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;div&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;id =&lt;/span&gt;&lt;span class=&quot;err&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;div1&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
	&lt;span class=&quot;nt&quot;&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Hello&lt;span class=&quot;nt&quot;&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;div&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;id =&lt;/span&gt;&lt;span class=&quot;err&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;div2&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
	&lt;span class=&quot;nt&quot;&gt;&amp;lt;p&amp;gt;&lt;/span&gt;World !&lt;span class=&quot;nt&quot;&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;显示结果是这样：&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Hello
World !
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;但是设置浮动css后：&lt;/p&gt;

&lt;div class=&quot;language-css highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;div&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;nl&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;left&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;效果就是这样：&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;HelloWorld !
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;也可以设置元素一左一右显示：&lt;/p&gt;

&lt;div class=&quot;language-css highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nf&quot;&gt;#div1&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;nl&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;left&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;nf&quot;&gt;#div2&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;nl&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;right&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;层模型layerposition&quot;&gt;层模型（layer）（position）&lt;/h4&gt;
&lt;p&gt;类似于PS中的&lt;strong&gt;图层&lt;/strong&gt;编辑，HTML中也存在层模型布局，对元素进行&lt;strong&gt;定位&lt;/strong&gt;。
层模型有三种：&lt;strong&gt;绝对定位&lt;/strong&gt;(absolute)、&lt;strong&gt;相对定位&lt;/strong&gt;(relative)、&lt;strong&gt;固定定位&lt;/strong&gt;(fixed)。&lt;/p&gt;

&lt;h5 id=&quot;绝对定位&quot;&gt;绝对定位&lt;/h5&gt;
&lt;p&gt;理解就是字面上的意思，简言之就是&lt;strong&gt;相对于上级设置了 position 属性的元素&lt;/strong&gt;进行定位，如果没有这类上级就是相对于 &lt;code class=&quot;highlighter-rouge&quot;&gt;body&lt;/code&gt; 标签，也是&lt;strong&gt;浏览器窗口&lt;/strong&gt;。需要设置css：&lt;code class=&quot;highlighter-rouge&quot;&gt;position: absolute;&lt;/code&gt;，然后就可以使用 &lt;code class=&quot;highlighter-rouge&quot;&gt;top&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;right&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;bottom&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;left&lt;/code&gt; 这类属性进行定位。例如：&lt;/p&gt;

&lt;div class=&quot;language-css highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;div&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;nl&quot;&gt;position&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;absolute&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;nl&quot;&gt;top&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;100px&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;nl&quot;&gt;left&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;150px&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;这样就使板块&lt;strong&gt;向下&lt;/strong&gt;移动100像素，&lt;strong&gt;向右&lt;/strong&gt;移动150像素。&lt;/p&gt;

&lt;h5 id=&quot;相对定位&quot;&gt;相对定位&lt;/h5&gt;
&lt;p&gt;这里的&lt;strong&gt;相对&lt;/strong&gt;较难理解，与数理中的“相对”不太一样，这里是&lt;strong&gt;“相对于自己原来应在的位置”&lt;/strong&gt;，需要设置css：&lt;code class=&quot;highlighter-rouge&quot;&gt;position:  relative;&lt;/code&gt;，重要的是不用关心&lt;strong&gt;上级是否设置了position属性&lt;/strong&gt;，这样就很方便。例如：&lt;/p&gt;

&lt;div class=&quot;language-css highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;div&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;nl&quot;&gt;position&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;relative&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;nl&quot;&gt;top&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;100px&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;nl&quot;&gt;right&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;100px&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;板块就相对于自己没设置样式前的位置，同时向左向下移动100px。&lt;/p&gt;

&lt;h5 id=&quot;固定定位&quot;&gt;固定定位&lt;/h5&gt;
&lt;p&gt;这个就好理解了，所谓&lt;strong&gt;固定&lt;/strong&gt;就是指固定于整个浏览器网页窗口不动，即使滚动网页内容也不改变位置，需要设置css：&lt;code class=&quot;highlighter-rouge&quot;&gt;position: fixed&lt;/code&gt;，也可以设置 &lt;code class=&quot;highlighter-rouge&quot;&gt;top&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;right&lt;/code&gt;等调整固定的位置。还记得浏览器某些网页右下角的小广告吗，是不是固定在那怎么浏览网页都不动 -_- .&lt;/p&gt;

            </description>
            <pubDate>Fri, 04 May 2018 00:00:00 +0000</pubDate>
            <guid>
                https://knightyun.github.io/2018/05/04/css-layout-model
            </guid>
        </item>
        
        <item>
            <title>CSS选择器详细介绍</title>
            <link>
                https://knightyun.github.io/2018/05/02/css-selector
            </link>
            <description>
                &lt;hr /&gt;
&lt;p&gt;文章出自个人博客&lt;a href=&quot;https://knightyun.github.io/2018/05/02/css-selector&quot;&gt;https://knightyun.github.io/2018/05/02/css-selector&lt;/a&gt;，转载请申明&lt;/p&gt;

&lt;hr /&gt;
&lt;h4 id=&quot;目录-&quot;&gt;目录 &lt;span id=&quot;home&quot;&gt;&lt;/span&gt;&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#1&quot;&gt;基础&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#2&quot;&gt;选择器&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#2.1&quot;&gt;元素选择器&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#2.2&quot;&gt;类选择器&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#2.3&quot;&gt;ID选择器&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#2.3.1&quot;&gt;ID选择器与类选择器的区别&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#2.4&quot;&gt;子选择器&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#2.5&quot;&gt;后代选择器&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#2.6&quot;&gt;通用选择器&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#2.7&quot;&gt;属性选择器&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#2.7.1&quot;&gt;简单属性选择器&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#2.7.2&quot;&gt;属性和值选择器&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#2.8&quot;&gt;伪元素选择器&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#2.8.1&quot;&gt;:active&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#2.8.2&quot;&gt;:hover&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#2.8.3&quot;&gt;:focus&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#2.8.4&quot;&gt;::selection&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#2.8.5&quot;&gt;:first-child&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#2.8.6&quot;&gt;:nth-child()&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#2.9&quot;&gt;组选择器&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#2.10&quot;&gt;相邻同级选择器&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;
&lt;h4 id=&quot;基础-&quot;&gt;基础 &lt;span id=&quot;1&quot;&gt;&lt;/span&gt;&lt;/h4&gt;
&lt;p&gt;CSS（层叠样式表Cascading Style Sheets）,用于修饰HTML网页内容，根据使用位置不同可分为三种样式：&lt;strong&gt;内联式，嵌入式，外部式。&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;内联式：&lt;/strong&gt;标签 &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;&amp;gt;&lt;/code&gt;内部使用，例如：&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;p style = &quot;color: red; font-size: 20px&quot;&amp;gt;&amp;lt;/p&amp;gt;&lt;/code&gt;。&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;嵌入式：&lt;/strong&gt;写在 &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;style&amp;gt;&amp;lt;/style&amp;gt;&lt;/code&gt; 之中，并放在 &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;head&amp;gt;&amp;lt;/head&amp;gt;&lt;/code&gt; 内，例如：&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-html highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;&amp;lt;!--这里放其它标签，例如 meta，link，script之类--&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;style&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;nl&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;red&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;nl&quot;&gt;font-size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;20px&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;外部式：&lt;/strong&gt;写在外部 &lt;code class=&quot;highlighter-rouge&quot;&gt;.css&lt;/code&gt; 文件中，使用如下方式引用：&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;link rel = &quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;css/index.css&quot;&amp;gt;&lt;/code&gt;
&lt;code class=&quot;highlighter-rouge&quot;&gt;href&lt;/code&gt; 写 &lt;code class=&quot;highlighter-rouge&quot;&gt;.css&lt;/code&gt; 文件路径，可以是绝对路径或相对路径，相对路径类似于：&lt;code class=&quot;highlighter-rouge&quot;&gt;../../css/index.css&lt;/code&gt;，绝对路径类似于：&lt;code class=&quot;highlighter-rouge&quot;&gt;/css/index.css&lt;/code&gt;。&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4 id=&quot;选择器selector&quot;&gt;选择器（Selector）&lt;span id=&quot;2&quot;&gt;&lt;/span&gt;&lt;/h4&gt;

&lt;p&gt;三种样式中，&lt;strong&gt;嵌入式&lt;/strong&gt;和&lt;strong&gt;外部式&lt;/strong&gt;需要使用到&lt;strong&gt;选择器&lt;/strong&gt;，也是组成 css 样式的主体例如上例中的 &lt;code class=&quot;highlighter-rouge&quot;&gt;p { }&lt;/code&gt;，主要分为：
元素选择器、类选择器、ID选择器、子选择器、后代选择器、通用选择器、属性选择器、伪元素选择器、组选择器、相邻同级选择器。&lt;/p&gt;

&lt;h5 id=&quot;元素选择器type-selector&quot;&gt;元素选择器（Type selector）&lt;span id=&quot;2.1&quot;&gt;&lt;/span&gt;&lt;/h5&gt;
&lt;p&gt;也叫类型选择器，可以理解为&lt;strong&gt;标签选择器&lt;/strong&gt;，最基本的选择器，就是使用常见的HTML元素，例如：&lt;code class=&quot;highlighter-rouge&quot;&gt;body { }&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;h { }&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;p { }&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;div { }&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;span { }&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;a { }&lt;/code&gt; 等。&lt;/p&gt;

&lt;h5 id=&quot;类选择器class-selector&quot;&gt;类选择器（Class selector）&lt;span id=&quot;2.2&quot;&gt;&lt;/span&gt;&lt;/h5&gt;
&lt;p&gt;&lt;strong&gt;前提&lt;/strong&gt;需要在标签内使用&lt;strong&gt;类&lt;/strong&gt;标记某处文档，类似：&lt;code class=&quot;highlighter-rouge&quot;&gt;class = &quot;myClass&quot;&lt;/code&gt;，然后它的选择器的格式就为：&lt;code class=&quot;highlighter-rouge&quot;&gt;.myClass { }&lt;/code&gt;，就是在类名前面加个小数点。&lt;/p&gt;

&lt;h5 id=&quot;id选择器id-selecctor&quot;&gt;ID选择器（ID selecctor）&lt;span id=&quot;2.3&quot;&gt;&lt;/span&gt;&lt;/h5&gt;
&lt;p&gt;和类选择器类似，&lt;strong&gt;前提&lt;/strong&gt;需要在标签内使用&lt;strong&gt;ID&lt;/strong&gt;标记某处文档，类似：&lt;code class=&quot;highlighter-rouge&quot;&gt;id = &quot;myId&quot;&lt;/code&gt;，格式为：&lt;code class=&quot;highlighter-rouge&quot;&gt;#myId { }&lt;/code&gt;，就是在 ID 前加个符号 &lt;code class=&quot;highlighter-rouge&quot;&gt;#&lt;/code&gt;。&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;ID选择器与类选择器的区别：&lt;/strong&gt; &lt;span id=&quot;2.3.1&quot;&gt;
类选择器可以使用多次，ID选择器只能使用一次，例如：
&lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;p class = &quot;one&quot;&amp;gt;This is a test content, &amp;lt;/p&amp;gt;&lt;/code&gt;
&lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;p class = &quot;one&quot;&amp;gt;hello world !&amp;lt;/p&amp;gt;&lt;/code&gt;，但是ID不能这样。
并且能同时使用多个类分别标记不同样式，例如：&lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;p class = &quot;one two&quot;&amp;gt;&amp;lt;/p&amp;gt;&lt;/code&gt;，ID也不能这样。&lt;/span&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h5 id=&quot;子选择器child-selector-&quot;&gt;子选择器（Child selector） &lt;span id=&quot;2.4&quot;&gt;&lt;/span&gt;&lt;/h5&gt;
&lt;p&gt;用于指定标签元素的&lt;strong&gt;子元素&lt;/strong&gt;，使用符号 &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;gt;&lt;/code&gt; 隔开父元素与子元素，例如：
HTML：&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;lt;p1&amp;gt;
Hello World !!!
&amp;lt;p2&amp;gt;This is a test content&amp;lt;/p2&amp;gt;
&amp;lt;/p1&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;CSS:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;p1&amp;gt;p2 {
color: green;
font-size: 20px;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;这里就指定了父元素 &lt;code class=&quot;highlighter-rouge&quot;&gt;p1&lt;/code&gt; 的子元素 &lt;code class=&quot;highlighter-rouge&quot;&gt;p2&lt;/code&gt; 的样式，但是&lt;strong&gt;只作用于子元素，不作用于父元素&lt;/strong&gt;。&lt;/p&gt;

&lt;h5 id=&quot;后代选择器descendant-selector-&quot;&gt;后代选择器（Descendant selector） &lt;span id=&quot;2.5&quot;&gt;&lt;/span&gt;&lt;/h5&gt;
&lt;p&gt;用于指定标签元素的&lt;strong&gt;后代元素&lt;/strong&gt;，使用空格符号隔开，例如：
html:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;lt;p1&amp;gt;
Hello world !!!
&amp;lt;p2&amp;gt;
This is a &amp;lt;a&amp;gt;test&amp;lt;/a&amp;gt; content
&amp;lt;/p2&amp;gt;
&amp;lt;/p1&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;css:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;p1 a {
color: green;
font-size: 20px;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;这里指定了元素 &lt;code class=&quot;highlighter-rouge&quot;&gt;p1&lt;/code&gt; 的后代元素 &lt;code class=&quot;highlighter-rouge&quot;&gt;a&lt;/code&gt; 的样式，注意这里不是&lt;strong&gt;子代元素&lt;/strong&gt; &lt;code class=&quot;highlighter-rouge&quot;&gt;p2&lt;/code&gt;，就是作用于&lt;strong&gt;所有指明的后代元素&lt;/strong&gt;。&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;子代选择器与后代选择器的区别：&lt;/strong&gt; &lt;span id=&quot;2.5.1&quot;&gt;
顾名思义的理解，后代就是包含子代在内的所有下代的元素，可以跨越子代直接作用于孙代；而子代只包含父级的&lt;strong&gt;第一代子代&lt;/strong&gt;元素。
子代选择器使用符号 &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;gt;&lt;/code&gt; 隔开，后代选择器使用&lt;code class=&quot;highlighter-rouge&quot;&gt;空格&lt;/code&gt; 隔开。&lt;/span&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h5 id=&quot;通用选择器-&quot;&gt;通用选择器 &lt;span id=&quot;2.6&quot;&gt;&lt;/span&gt;&lt;/h5&gt;
&lt;p&gt;顾名思义，使用通配符 &lt;code class=&quot;highlighter-rouge&quot;&gt;*&lt;/code&gt; 设置 html 中&lt;strong&gt;所有标签&lt;/strong&gt;的样式，例如：&lt;/p&gt;

&lt;div class=&quot;language-css highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;nl&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;red&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;nl&quot;&gt;font-size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;20px&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;这样就设置了HTML中所有的标签的样式了。&lt;/p&gt;

&lt;h5 id=&quot;属性选择器attribute-selector-&quot;&gt;属性选择器（Attribute selector） &lt;span id=&quot;2.7&quot;&gt;&lt;/span&gt;&lt;/h5&gt;
&lt;p&gt;对&lt;strong&gt;具有指定的属性&lt;/strong&gt;(attribute)设置样式，使用方括号符号 &lt;code class=&quot;highlighter-rouge&quot;&gt;[ ]&lt;/code&gt;。&lt;/p&gt;

&lt;h6 id=&quot;简单属性选择器-&quot;&gt;简单属性选择器 &lt;span id=&quot;2.7.1&quot;&gt;&lt;/span&gt;&lt;/h6&gt;
&lt;p&gt;不用管属性值，例如：&lt;/p&gt;

&lt;div class=&quot;language-css highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;href&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;red&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;href&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;red&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;href&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;title&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;red&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;blockquote&gt;
  &lt;p&gt;以上格式都能实现相同效果，即具有该属性的 &lt;code class=&quot;highlighter-rouge&quot;&gt;a&lt;/code&gt; 标签。&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h6 id=&quot;属性和值选择器-&quot;&gt;属性和值选择器 &lt;span id=&quot;2.7.2&quot;&gt;&lt;/span&gt;&lt;/h6&gt;
&lt;p&gt;具有指定属性与其&lt;strong&gt;指定值&lt;/strong&gt;的标签，格式为：&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;a [title = &quot;link&quot;] {color: red}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h5 id=&quot;伪元素选择器-&quot;&gt;伪元素选择器 &lt;span id=&quot;2.8&quot;&gt;&lt;/span&gt;&lt;/h5&gt;
&lt;p&gt;HTML中存在一类与元素控制内容相同的&lt;strong&gt;抽象元素&lt;/strong&gt;，但是并不实际存在于HTML文档，给标签的&lt;strong&gt;某种状态&lt;/strong&gt;设置样式，例如单击某内容或鼠标滑过某内容，然后设置改变的样式。伪元素种类较多，只列举几个常用例子。&lt;/p&gt;

&lt;h6 id=&quot;active-&quot;&gt;:active &lt;span id=&quot;2.8.1&quot;&gt;&lt;/span&gt;&lt;/h6&gt;
&lt;p&gt;为&lt;strong&gt;激活&lt;/strong&gt;的元素设置样式，就是用户单击该标签时的样式，例如：&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;a:active {color: red}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;则用户点击这个链接文本时颜色变为红色。&lt;/p&gt;

&lt;h6 id=&quot;hover-&quot;&gt;:hover &lt;span id=&quot;2.8.2&quot;&gt;&lt;/span&gt;&lt;/h6&gt;
&lt;p&gt;悬停状态伪元素，为用户鼠标所&lt;strong&gt;停靠&lt;/strong&gt;的标签设置样式，例如：&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;p:hover {color: red}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;则鼠标停留在该段落时，段落内容变为红色。&lt;/p&gt;

&lt;h6 id=&quot;focus-&quot;&gt;:focus &lt;span id=&quot;2.8.3&quot;&gt;&lt;/span&gt;&lt;/h6&gt;
&lt;p&gt;用于具有&lt;strong&gt;焦点&lt;/strong&gt;的元素，常用就是输入框，用户点击输入框准备输入时则该输入框就具有了焦点，例如：&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;input:focus {background-color: green} 则点击输入框时背景颜色变为绿色。
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h6 id=&quot;selection-&quot;&gt;::selection &lt;span id=&quot;2.8.4&quot;&gt;&lt;/span&gt;&lt;/h6&gt;
&lt;p&gt;为选中的元素设置样式，例如：&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;::selection {color: red}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;为文档中鼠标选定的内容设置为红色字体。注意可以追加应用范围：&lt;code class=&quot;highlighter-rouge&quot;&gt;p::selection&lt;/code&gt; 表示段落中选中的字体才应用该样式，不追加直接使用 &lt;code class=&quot;highlighter-rouge&quot;&gt;::selection&lt;/code&gt; 表示应用于所有内容。
&lt;strong&gt;火狐浏览器&lt;/strong&gt;支持需要使用 &lt;code class=&quot;highlighter-rouge&quot;&gt;-moz-selection&lt;/code&gt;。&lt;/p&gt;

&lt;h6 id=&quot;first-child-&quot;&gt;:first-child &lt;span id=&quot;2.8.5&quot;&gt;&lt;/span&gt;&lt;/h6&gt;
&lt;p&gt;为元素的第一子代应用样式，例如：&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;p:first-child {color: red}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;这个比较好理解。&lt;/p&gt;

&lt;h6 id=&quot;nth-child-&quot;&gt;:nth-child() &lt;span id=&quot;2.8.6&quot;&gt;&lt;/span&gt;&lt;/h6&gt;
&lt;p&gt;为元素的&lt;strong&gt;父元素&lt;/strong&gt;的第 n 个子代设置样式，只是括号内需要输入数字表示第几代，例如：&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;p:nth-child(2) {color: red}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;假如p标签父元素是body，就表示为body的第二个子元素设置样式。&lt;/p&gt;

&lt;h5 id=&quot;组选择器-&quot;&gt;组选择器 &lt;span id=&quot;2.9&quot;&gt;&lt;/span&gt;&lt;/h5&gt;
&lt;p&gt;为多个元素设置相同样式时，可以使用&lt;strong&gt;逗号&lt;/strong&gt;分隔元素，达到同时设置的效果。例如：&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;p1,p1 {color: red}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;表示为p1和p2 &lt;strong&gt;同时&lt;/strong&gt;设置&lt;strong&gt;相同&lt;/strong&gt;的样式。&lt;/p&gt;

&lt;h5 id=&quot;相邻同级选择器-&quot;&gt;相邻同级选择器 &lt;span id=&quot;2.10&quot;&gt;&lt;/span&gt;&lt;/h5&gt;
&lt;p&gt;选择与指定元素同级的相邻的第一个某元素，设置样式，例如：&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;div+p {color: red}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;表示为 p 设置样式，并且这个 p 与 div 同级，并且是与 div &lt;strong&gt;相邻的第一个&lt;/strong&gt; p，div &lt;strong&gt;内部&lt;/strong&gt;的 p 并不包含在内。&lt;/p&gt;

&lt;hr /&gt;
&lt;h4 id=&quot;返回顶部&quot;&gt;返回&lt;a href=&quot;#home&quot;&gt;顶部&lt;/a&gt;&lt;/h4&gt;

            </description>
            <pubDate>Wed, 02 May 2018 00:00:00 +0000</pubDate>
            <guid>
                https://knightyun.github.io/2018/05/02/css-selector
            </guid>
        </item>
        
        <item>
            <title>C语言之变量存储类型与链接属性</title>
            <link>
                https://knightyun.github.io/2018/04/28/c-storage-type
            </link>
            <description>
                &lt;p&gt;C语言中一个重要的东西就是弄清申明变量的类型、作用域、存储类型、链接属性等，例如是整型还是浮点型，存储于普通内存还是堆栈或者寄存器，作用于全局还是局部，能否被其他文件 引用等。&lt;/p&gt;

&lt;h4 id=&quot;链接属性&quot;&gt;链接属性&lt;/h4&gt;

&lt;p&gt;申明变量或函数时需要&lt;strong&gt;标识符&lt;/strong&gt;，标识符的链接属性一共有三种：&lt;code class=&quot;highlighter-rouge&quot;&gt;external(外部)&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;internal(内部)&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;none(无)&lt;/code&gt;，external和internal常用，&lt;strong&gt;none&lt;/strong&gt;表示无链接属性，该标识符的多个申明被当成独立不同的实体。&lt;/p&gt;

&lt;p&gt;顾名思义，&lt;strong&gt;external&lt;/strong&gt;表示能被其它源文件访问的变量或函数，&lt;strong&gt;internal&lt;/strong&gt;则不能被其它源文件访问，并且&lt;strong&gt;缺省&lt;/strong&gt;情况下&lt;strong&gt;代码块(block)外部&lt;/strong&gt;的变量为&lt;strong&gt;external&lt;/strong&gt;属性，也就是外部变量，&lt;strong&gt;代码块内部&lt;/strong&gt;的变量为&lt;strong&gt;internal&lt;/strong&gt;属性，即局部变量.&lt;/p&gt;

&lt;p&gt;如果需要改变链接属性需要使用 &lt;code class=&quot;highlighter-rouge&quot;&gt;extern&lt;/code&gt; 和 &lt;code class=&quot;highlighter-rouge&quot;&gt;static&lt;/code&gt; 关键字，&lt;code class=&quot;highlighter-rouge&quot;&gt;extern&lt;/code&gt; 是转化为&lt;strong&gt;external&lt;/strong&gt;属性，&lt;code class=&quot;highlighter-rouge&quot;&gt;static&lt;/code&gt; 是转化为&lt;strong&gt;internal&lt;/strong&gt;属性。
举例说明：&lt;/p&gt;

&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cp&quot;&gt;#include&amp;lt;stdio.h&amp;gt;
&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;              &lt;span class=&quot;cm&quot;&gt;/* 这里是代码块外部，external 属性，缺省为&quot;extern&quot; */&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;extern&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;aa&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;11&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;     &lt;span class=&quot;cm&quot;&gt;/* 与上面效果一样 */&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;aaa&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;111&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;   &lt;span class=&quot;cm&quot;&gt;/* 这里申明的是 internal 属性，不能被其它源文件访问 */&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;example&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;          &lt;span class=&quot;cm&quot;&gt;/* 这里申明的 example() 函数也是external属性，能被其它源文件访问 */&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;          &lt;span class=&quot;cm&quot;&gt;/* 这里是代码块内部，internal 属性 */&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;extern&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bb&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;22&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;cm&quot;&gt;/* 这里的意思是访问其它源文件的全局变量 */&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;注意上面指出的代码块内部和外部，定义的函数的花括号之外叫做代码块外部，之内叫做代码块内部。&lt;/strong&gt;
&lt;strong&gt;代码块外部缺省为&lt;code class=&quot;highlighter-rouge&quot;&gt;extern&lt;/code&gt;，并且代码块外部使用&lt;code class=&quot;highlighter-rouge&quot;&gt;extern&lt;/code&gt;表示被其它源文件访问，代码块内部使用&lt;code class=&quot;highlighter-rouge&quot;&gt;extern&lt;/code&gt;表示访问其它文件的外部变量。&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4 id=&quot;存储类型&quot;&gt;存储类型&lt;/h4&gt;

&lt;p&gt;存储类型指申明的变量将被存储到什么地方去，并且与其存储周期有关，就是这个变量何时被创建，何时被销毁，保持多久。&lt;/p&gt;

&lt;p&gt;存储类型有三种：
静态变量(&lt;code class=&quot;highlighter-rouge&quot;&gt;static&lt;/code&gt;) —&amp;gt; 存储于普通内存。
自动变量(&lt;code class=&quot;highlighter-rouge&quot;&gt;auto&lt;/code&gt;)   —&amp;gt; 存储于堆栈。
寄存器变量(&lt;code class=&quot;highlighter-rouge&quot;&gt;register&lt;/code&gt;) —&amp;gt; 存储于寄存器。&lt;/p&gt;

&lt;h6 id=&quot;静态变量&quot;&gt;静态变量&lt;/h6&gt;
&lt;p&gt;任何代码块之外定义的变量，总是处于静态内存中，无需使用 &lt;code class=&quot;highlighter-rouge&quot;&gt;static&lt;/code&gt; 关键字，在程序运行&lt;strong&gt;之前&lt;/strong&gt;创建，程序整个执行期间&lt;strong&gt;始终存在&lt;/strong&gt;。&lt;/p&gt;

&lt;h6 id=&quot;自动变量&quot;&gt;自动变量&lt;/h6&gt;
&lt;p&gt;代码块内部申明的变量，并且&lt;strong&gt;缺省&lt;/strong&gt;情况下都是(&lt;code class=&quot;highlighter-rouge&quot;&gt;auto&lt;/code&gt;)类型，所以很少使用，存储于堆栈中，程序执行到申明变量处被创建，离开后被销毁，每次在堆栈中占据的内存位置都&lt;strong&gt;可能不同&lt;/strong&gt;。&lt;/p&gt;

&lt;h6 id=&quot;寄存器变量&quot;&gt;寄存器变量&lt;/h6&gt;
&lt;p&gt;用于申明&lt;strong&gt;自动变量&lt;/strong&gt;，即在&lt;strong&gt;代码块内部&lt;/strong&gt;使用，使这类变量存储于寄存器中，&lt;strong&gt;寄存器中的变量比内存中的变量访问效率更高&lt;/strong&gt;。
但是，如果有&lt;strong&gt;太多&lt;/strong&gt;变量申明为 &lt;code class=&quot;highlighter-rouge&quot;&gt;register&lt;/code&gt; ，只会选取前几个存储于寄存器中，其它处理为普通自动变量。&lt;/p&gt;

&lt;p&gt;通常把&lt;strong&gt;使用频率最高&lt;/strong&gt;的变量申明为寄存器变量，或者指针申明为寄存器变量，以提高效率。例如可以把函数的形参申明为寄存器变量（有可能它节省的时间空间开销抵不上复制这几个值的开销）。&lt;/p&gt;

&lt;h6 id=&quot;综合举例&quot;&gt;综合举例&lt;/h6&gt;

&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cp&quot;&gt;#include&amp;lt;stdio.h&amp;gt;
&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;                  &lt;span class=&quot;cm&quot;&gt;/* 静态变量(static)，这里不能使用 register */&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;              &lt;span class=&quot;cm&quot;&gt;/* 自动变量，省略 auto */&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;register&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;     &lt;span class=&quot;cm&quot;&gt;/* 寄存器变量(register) */&lt;/span&gt;
	&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;aaa&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;register&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;cm&quot;&gt;/* 申明函数形参为 register */&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;       &lt;span class=&quot;cm&quot;&gt;/* 申明静态变量，和变量 b 一个属性 */&lt;/span&gt; 
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h6 id=&quot;初始化&quot;&gt;初始化&lt;/h6&gt;
&lt;p&gt;静态变量在不指定初始值的时候，初始化为 &lt;code class=&quot;highlighter-rouge&quot;&gt;0&lt;/code&gt;；
自动变量是否初始化赋值，并无效率的改变（每次执行都要重新初始化），这也是它的优点：可以用任何“表达式”作为初始值。例如：&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;int add(int a)
{
	int b = a + 1;      /* 将表达式 a + 1 的值初始化给 b */
	return b;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;注意&quot;&gt;注意&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;static&lt;/code&gt; 申明函数或代码块之外的变量时，只修改&lt;strong&gt;链接属性&lt;/strong&gt; ``external&lt;code class=&quot;highlighter-rouge&quot;&gt; 为 &lt;/code&gt;internal`，&lt;strong&gt;存储类型&lt;/strong&gt;和&lt;strong&gt;作用域&lt;/strong&gt;不变。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;static&lt;/code&gt; 申明代码块内部变量时，将自动变量修改为&lt;strong&gt;静态变量&lt;/strong&gt;，但是&lt;strong&gt;链接属性&lt;/strong&gt;和&lt;strong&gt;作用域&lt;/strong&gt;不变。&lt;/li&gt;
&lt;/ul&gt;

&lt;h6 id=&quot;综合举例-1&quot;&gt;综合举例&lt;/h6&gt;

&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cp&quot;&gt;#include&amp;lt;stdio.h&amp;gt;
&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;             &lt;span class=&quot;cm&quot;&gt;/* 链接属性为 external，缺省 extern */&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;      &lt;span class=&quot;cm&quot;&gt;/* 修改连接属性为静态变量，不能被其它源文件访问，依然为全局变量，存储于静态内存中 */&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;         &lt;span class=&quot;cm&quot;&gt;/* 自动变量，存储到堆栈 */&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;  &lt;span class=&quot;cm&quot;&gt;/* 静态变量，存储到普通内存，依然不能被其它源文件访问 */&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

            </description>
            <pubDate>Sat, 28 Apr 2018 00:00:00 +0000</pubDate>
            <guid>
                https://knightyun.github.io/2018/04/28/c-storage-type
            </guid>
        </item>
        
        <item>
            <title>C语言之随机数函数(rand())的使用方法</title>
            <link>
                https://knightyun.github.io/2018/04/25/c-rand-number
            </link>
            <description>
                &lt;p&gt;在程序设计中，难免会使用到随机值函数，其原理与语法大多类似，接下来以C语言为例介绍其随机值函数 &lt;code class=&quot;highlighter-rouge&quot;&gt;rand()&lt;/code&gt; 用法。&lt;/p&gt;

&lt;h4 id=&quot;原理&quot;&gt;原理&lt;/h4&gt;
&lt;p&gt;引用&lt;a href=&quot;https://baike.baidu.com/item/rand%28%29&quot;&gt;百度百科&lt;/a&gt;，首先，需要包含头文件：&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;#include &amp;lt;stdlib.h&amp;gt; rand()函数是按指定的顺序来产生整数，因此每次执行上面的语句都打印相同的两个值，所以说C语言的随机并不是真正意义上的随机，有时候也叫[伪随机数][wei]，使用 `rand()` 生成随机数之前需要用随机发生器的初始化函数 `srand(unsigned seed)`（也位于 `stdlib.h` 中） 进行伪随机数序列初始化，`seed` 又叫[随机种子][seed]，通俗讲就是，如果每次提供的 `seed` 是一样的话，最后每一轮生成的几个随机值也都是一样的，因此叫伪随机数，所以需要每次提供不同的 `seed` 达到完全的随机，我们通常用时间函数 `time(NULL)` 作为 `seed` ，因为时间值每秒都不同，这个函数需要包含以下头文件：

#include &amp;lt;time.h&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;理论太泛，下面用例子分析理解。&lt;/p&gt;

&lt;h4 id=&quot;举例分析&quot;&gt;举例分析&lt;/h4&gt;
&lt;p&gt;先来理解以下伪随机数，编译以下代码：&lt;/p&gt;
&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cp&quot;&gt;#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;stdlib.h&amp;gt;
#include &amp;lt;time.h&amp;gt;
&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;srand&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;%d, &quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rand&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;11&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; 
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;运行结果&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;8, 9, 9, 1, 7, 5, 5, 10, 1, 0,
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后无论运行多少次，结果都依然是以上随机数，不会改变，因为每次设置的种子 &lt;code class=&quot;highlighter-rouge&quot;&gt;seed&lt;/code&gt; 都是 &lt;code class=&quot;highlighter-rouge&quot;&gt;1&lt;/code&gt; 。&lt;/p&gt;

&lt;p&gt;但是假如把 &lt;code class=&quot;highlighter-rouge&quot;&gt;seed&lt;/code&gt; 换成 &lt;code class=&quot;highlighter-rouge&quot;&gt;time(NULL)&lt;/code&gt;，每次就不一样了，如下：&lt;/p&gt;
&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cp&quot;&gt;#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;stdlib.h&amp;gt;
#include &amp;lt;time.h&amp;gt;
&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;srand&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
	&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;%d, &quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rand&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;11&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; 
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;结果是就变了，并且每次都不一样：&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;6, 3, 4, 5, 5, 9, 8, 10, 10, 4,
6, 4, 2, 4, 3, 2, 5, 1, 2, 9,
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;这里的 &lt;code class=&quot;highlighter-rouge&quot;&gt;time(NULL)&lt;/code&gt; 的结果是一个类似于 &lt;code class=&quot;highlighter-rouge&quot;&gt;1524655706&lt;/code&gt; 的数字，并且每秒都在递增 1，也就达成了 srand() 的 seed 不断变化的目的，不断生成新的随机数。&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4 id=&quot;拓展&quot;&gt;拓展&lt;/h4&gt;
&lt;p&gt;这里注意一下例子中函数 &lt;code class=&quot;highlighter-rouge&quot;&gt;rand()&lt;/code&gt; 的用法，函数括号内不需要加参数，如果直接调用 &lt;code class=&quot;highlighter-rouge&quot;&gt;rand()&lt;/code&gt; 的话会生成下面这样的数：&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;17163, 2663, 24810, 4875, 26975, 14119, 22193, 11233, 26009, 20105, 所以我们想要生成指定范围的随机数的话就需要使用到**求余**运算符 `%`，这里有个规律：例如我们需要 0--10的随机数时，就写成 `rand()%11`，0--100就写成 `rand()%101`，就是运算符后的数字需要比需求范围极值大 1，当然这也是取余运算的原理。
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;hr /&gt;

            </description>
            <pubDate>Wed, 25 Apr 2018 00:00:00 +0000</pubDate>
            <guid>
                https://knightyun.github.io/2018/04/25/c-rand-number
            </guid>
        </item>
        
        <item>
            <title>贪吃蛇C语言源码与算法分析</title>
            <link>
                https://knightyun.github.io/2018/04/24/c-gluttonous-snake
            </link>
            <description>
                &lt;p&gt;经典的贪吃蛇游戏算法，无疑是一个较大的挑战，综合性较高，像我这种刚入门C语言的也差不多花了整整一周时间才差不多理解透彻，内部包含了较多的函数，数组，二维数组，循环等思想。
Github项目地址：&lt;a href=&quot;https://github.com/knightyun/gluttonousSnake/&quot;&gt;https://github.com/knightyun/gluttonousSnake/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;接下来以C语言为例，针对此算法截取代码片段进行详细分析，源码位于文章&lt;a href=&quot;#end&quot;&gt;底部&lt;/a&gt;。&lt;/p&gt;

&lt;h4 id=&quot;算法分析&quot;&gt;算法分析&lt;/h4&gt;
&lt;h6 id=&quot;概述&quot;&gt;概述&lt;/h6&gt;
&lt;p&gt;首先分析一下，贪吃蛇最基本和重要的动作，一段在屏幕上移动和转向的躯干，但是C语言没有移动字符的函数，只能不断向屏幕打印输出和清屏实现移动，躯干位置在屏幕上的变化可以用二维坐标系实现，用一个二维数组保存屏幕所有可见内容的x，y坐标，并赋予几种初始值，然后用函数打印出各种值对应的字符，再用循环和坐标值自增自减实现移动。使用输入函数判断方向，随机函数生成食物，根据头部坐标判断撞墙或吃到自己而结束游戏。&lt;/p&gt;

&lt;h6 id=&quot;头文件&quot;&gt;头文件&lt;/h6&gt;
&lt;p&gt;Windows环境中需要包含的头文件：&lt;/p&gt;
&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cp&quot;&gt;#include&amp;lt;stdio.h&amp;gt;     
#include&amp;lt;stdlib.h&amp;gt; 
#include&amp;lt;Windows.h&amp;gt;       &lt;/span&gt;&lt;span class=&quot;cm&quot;&gt;/*需要使用system(&quot;cls&quot;)清屏函数*/&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
#include&amp;lt;conio.h&amp;gt;         &lt;/span&gt;&lt;span class=&quot;cm&quot;&gt;/*非标准库函数，VS中自带，需要使用_getch()函数获取输入*/&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
#include&amp;lt;time.h&amp;gt;          &lt;/span&gt;&lt;span class=&quot;cm&quot;&gt;/*使用随机数函数需要使用time()函数*/&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h6 id=&quot;预定义&quot;&gt;预定义&lt;/h6&gt;
&lt;p&gt;游戏内可自定义设置界面宽度与高度，需要给二维数组定义一个最大值：&lt;/p&gt;
&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cp&quot;&gt;#define MAXX 10000                &lt;/span&gt;&lt;span class=&quot;cm&quot;&gt;/* 定义游戏最大界面宽度坐标值 */&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
#define MAXY 10000                &lt;/span&gt;&lt;span class=&quot;cm&quot;&gt;/* 定义最大高度 */&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;h6 id=&quot;申明变量&quot;&gt;申明变量&lt;/h6&gt;
&lt;p&gt;需要用到的变量和功能如下：&lt;/p&gt;
&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;speed&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;                    &lt;span class=&quot;cm&quot;&gt;/* 设置蛇移动速度 */&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mapArr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MAXX&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MAXY&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;            &lt;span class=&quot;cm&quot;&gt;/* 保存界面坐标值的二维数组 */&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;inputX&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;inputY&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;      &lt;span class=&quot;cm&quot;&gt;/* 默认游戏界面宽度和高度 */&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;randX&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;randY&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;        &lt;span class=&quot;cm&quot;&gt;/* 生成的随机食物的坐标值 */&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;foodFlag&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;                  &lt;span class=&quot;cm&quot;&gt;/* 判断是否更新食物 */&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sx&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sy&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;				   &lt;span class=&quot;cm&quot;&gt;/* 设置蛇身坐标值 */&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;l&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;				           &lt;span class=&quot;cm&quot;&gt;/* 蛇身长度 */&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MAXX&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MAXY&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;			   &lt;span class=&quot;cm&quot;&gt;/* 储存蛇身的指针数组 */&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;input&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&#39;6&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;			       &lt;span class=&quot;cm&quot;&gt;/* 默认移动方向 */&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;overFlag&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;                  &lt;span class=&quot;cm&quot;&gt;/* 判断游戏结束，撞墙或吃到自己 */&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;moveX&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;moveY&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;          &lt;span class=&quot;cm&quot;&gt;/* 游戏开始的动画效果坐标值 */&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;moveFlag&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;                  &lt;span class=&quot;cm&quot;&gt;/* 开始动画的循环判断 */&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;这里使用了指针数组控制蛇身，方便赋值&lt;/strong&gt;
&lt;strong&gt;数组中的参数虽然不能是变量，但是可以是宏定义&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h6 id=&quot;函数&quot;&gt;函数&lt;/h6&gt;
&lt;p&gt;接下来就是重要环节了，分析实现游戏效果的各个函数。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;第一步，定义初始化函数 &lt;code class=&quot;highlighter-rouge&quot;&gt;InitMap()&lt;/code&gt; 将屏幕上每个点通过二维数组赋予坐标值 x、y，确定游戏界面的大小，我们将四周的墙赋值为 &lt;code class=&quot;highlighter-rouge&quot;&gt;1&lt;/code&gt;，中间空白赋值为 &lt;code class=&quot;highlighter-rouge&quot;&gt;0&lt;/code&gt;，蛇身赋值为 &lt;code class=&quot;highlighter-rouge&quot;&gt;2&lt;/code&gt;，随机出现的食物赋值为 &lt;code class=&quot;highlighter-rouge&quot;&gt;3&lt;/code&gt;。&lt;/li&gt;
  &lt;li&gt;第二步，定义 &lt;code class=&quot;highlighter-rouge&quot;&gt;PrintMap()&lt;/code&gt; 函数给每个 坐标值打印对应的字符，我们将墙用字符 &lt;code class=&quot;highlighter-rouge&quot;&gt;+&lt;/code&gt; 表示，空白用空字符 &lt;code class=&quot;highlighter-rouge&quot;&gt;&quot; &quot;&lt;/code&gt; 表示，蛇身用星号 &lt;code class=&quot;highlighter-rouge&quot;&gt;*&lt;/code&gt; 表示，食物用字符 &lt;code class=&quot;highlighter-rouge&quot;&gt;@&lt;/code&gt; 表示。&lt;/li&gt;
  &lt;li&gt;第三步，定义函数 &lt;code class=&quot;highlighter-rouge&quot;&gt;StartMsg()&lt;/code&gt; 显示屏幕信息，提示控制的按键。&lt;/li&gt;
  &lt;li&gt;第四步，定义函数 &lt;code class=&quot;highlighter-rouge&quot;&gt;GetSet()&lt;/code&gt; 使玩家可以自定义游戏界面宽和高和移动速度，使用 &lt;code class=&quot;highlighter-rouge&quot;&gt;scanf()&lt;/code&gt; 函数获取并改变默认界面尺寸。&lt;/li&gt;
  &lt;li&gt;第五步，定义函数 &lt;code class=&quot;highlighter-rouge&quot;&gt;SetRandNum()&lt;/code&gt; 在界面中随机出现食物，并且不与蛇身和墙重叠，使用 &lt;code class=&quot;highlighter-rouge&quot;&gt;srand(time(0))&lt;/code&gt; 初始随机函数，然后用一个循环不断用随机函数 &lt;code class=&quot;highlighter-rouge&quot;&gt;rand()&lt;/code&gt; 生成随机坐标值，直到所生成位置是空白为止。&lt;/li&gt;
  &lt;li&gt;第六步，最核心的算法，较为复杂，定义函数 &lt;code class=&quot;highlighter-rouge&quot;&gt;SetSnakeNum()&lt;/code&gt; 设置蛇身的坐标值，并通过通过输入判断前进方向，以设置的速度时间间隔不断自增或自减坐标值实现移动，里面用到了 &lt;code class=&quot;highlighter-rouge&quot;&gt;_kbhit()&lt;/code&gt; 函数，作用是：有用户输入时，返回值为&lt;strong&gt;真&lt;/strong&gt;，无输入时值为&lt;strong&gt;假&lt;/strong&gt;。还有 &lt;code class=&quot;highlighter-rouge&quot;&gt;_getch()&lt;/code&gt; 函数与 &lt;code class=&quot;highlighter-rouge&quot;&gt;getchar()&lt;/code&gt; 的区别：&lt;code class=&quot;highlighter-rouge&quot;&gt;_getch()&lt;/code&gt; 输入值后不用输入回车就能获取输入值。感觉最不好理解的就是蛇身的转弯算法，我的方法是：&lt;strong&gt;蛇身每一节在每一次循环不断继承前一节的值&lt;/strong&gt;，然后蛇头位置不断获得新坐标值，这样就能实现身体的转向，这里就可以用到之前定义的指针数组 &lt;code class=&quot;highlighter-rouge&quot;&gt;*body[]&lt;/code&gt; 来实现。算法中还需要&lt;strong&gt;注意&lt;/strong&gt;的一点是，蛇身朝某个方向移动时，只能控制另外两个方向，例如向右移动时不能控制向左移动。然后随后的函数就好说了。&lt;/li&gt;
  &lt;li&gt;第七步，定义函数 &lt;code class=&quot;highlighter-rouge&quot;&gt;EatFood()&lt;/code&gt; 实现遇到食物坐标值时，增加一节蛇身长度值 &lt;code class=&quot;highlighter-rouge&quot;&gt;l&lt;/code&gt; 。&lt;/li&gt;
  &lt;li&gt;第八步，定义函数 &lt;code class=&quot;highlighter-rouge&quot;&gt;StartGame()&lt;/code&gt; 来综合之前的函数并开始游戏，需要通过 &lt;code class=&quot;highlighter-rouge&quot;&gt;overFlag&lt;/code&gt; 判断游戏结束。&lt;/li&gt;
  &lt;li&gt;第九步，定义函数 &lt;code class=&quot;highlighter-rouge&quot;&gt;SetMoveNum()&lt;/code&gt; 实现游戏开始时的动画效果，对于游戏存在意义不大，仅供研究训练思维和算法。&lt;/li&gt;
  &lt;li&gt;第十步，定义函数 &lt;code class=&quot;highlighter-rouge&quot;&gt;JudgeEnd()&lt;/code&gt; 判断之前的函数 &lt;code class=&quot;highlighter-rouge&quot;&gt;SetMuveNum()&lt;/code&gt; 的结束时刻，然后不断循环动画效果。&lt;/li&gt;
  &lt;li&gt;第十一步，定义函数 &lt;code class=&quot;highlighter-rouge&quot;&gt;StartView()&lt;/code&gt; 开始游戏，按任意键游戏正式开始。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;下面是函数申明：&lt;/p&gt;

&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;InitMap&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;                   &lt;span class=&quot;cm&quot;&gt;/* initialize the background coordinate system */&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PrintMap&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;                  &lt;span class=&quot;cm&quot;&gt;/* print every point in the arr mapArr to the screen */&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;StartMsg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;                  &lt;span class=&quot;cm&quot;&gt;/* start message */&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;GetSet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;                    &lt;span class=&quot;cm&quot;&gt;/* judge whether to edit the game setting */&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SetRandNum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;                &lt;span class=&quot;cm&quot;&gt;/* set a random &#39;food&#39; point in the screen */&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SetSnakeNum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;               &lt;span class=&quot;cm&quot;&gt;/* the most complex and important algorithm of this game */&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;EatFood&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;                   &lt;span class=&quot;cm&quot;&gt;/* judge when to eat food and elongate the body */&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;StartGame&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;                 &lt;span class=&quot;cm&quot;&gt;/* start the game */&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SetMoveNum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;                &lt;span class=&quot;cm&quot;&gt;/* algorithm of the start animation, some complex */&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;JudgeEnd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;                  &lt;span class=&quot;cm&quot;&gt;/* judge the end of the animation and loop again*/&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;StartView&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;                 &lt;span class=&quot;cm&quot;&gt;/* start the start animation view */&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;源码-&quot;&gt;源码 &lt;span id=&quot;end&quot;&gt;&lt;/span&gt;&lt;/h4&gt;
&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cp&quot;&gt;#include&amp;lt;stdio.h&amp;gt;
#include&amp;lt;stdlib.h&amp;gt;
#include&amp;lt;Windows.h&amp;gt;                &lt;/span&gt;&lt;span class=&quot;cm&quot;&gt;/* use the function &#39;system(&quot;cls&quot;)&#39; to clear screen */&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
#include&amp;lt;conio.h&amp;gt;                 &lt;/span&gt;&lt;span class=&quot;cm&quot;&gt;/* use the function &#39;_getch()&#39; to get input */&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
#include&amp;lt;time.h&amp;gt;                  &lt;/span&gt;&lt;span class=&quot;cm&quot;&gt;/* the random function need the &#39;time()&#39; function */&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
#include &quot;public-fun.h&quot;
&lt;/span&gt;
&lt;span class=&quot;cp&quot;&gt;#define MAXX 10000                &lt;/span&gt;&lt;span class=&quot;cm&quot;&gt;/* define the max width of game space */&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
#define MAXY 10000                &lt;/span&gt;&lt;span class=&quot;cm&quot;&gt;/* define the max height */&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;InitMap&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;                   &lt;span class=&quot;cm&quot;&gt;/* initialize the background coordinate system */&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PrintMap&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;                  &lt;span class=&quot;cm&quot;&gt;/* print every point in the arr mapArr to the screen */&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;StartMsg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;                  &lt;span class=&quot;cm&quot;&gt;/* start message */&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;GetSet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;                    &lt;span class=&quot;cm&quot;&gt;/* judge whether to edit the game setting */&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SetRandNum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;                &lt;span class=&quot;cm&quot;&gt;/* set a random &#39;food&#39; point in the screen */&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SetSnakeNum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;               &lt;span class=&quot;cm&quot;&gt;/* the most complex and important algorithm of this game */&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;EatFood&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;                   &lt;span class=&quot;cm&quot;&gt;/* judge when to eat food and elongate the body */&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;StartGame&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;                 &lt;span class=&quot;cm&quot;&gt;/* start the game */&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SetMoveNum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;                &lt;span class=&quot;cm&quot;&gt;/* algorithm of the start animation, some complex */&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;JudgeEnd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;                  &lt;span class=&quot;cm&quot;&gt;/* judge the end of the animation and loop again*/&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;StartView&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;                 &lt;span class=&quot;cm&quot;&gt;/* start the start animation view */&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;speed&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;                   &lt;span class=&quot;cm&quot;&gt;/* default snake move speed */&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mapArr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MAXX&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MAXY&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;            &lt;span class=&quot;cm&quot;&gt;/* arr to store the point in the screen */&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;inputX&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;inputY&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;      &lt;span class=&quot;cm&quot;&gt;/* default width and height */&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;randX&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;randY&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;        &lt;span class=&quot;cm&quot;&gt;/* set a random food point in the background */&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;foodFlag&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;                  &lt;span class=&quot;cm&quot;&gt;/* judge when to change random food point*/&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sx&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sy&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;				&lt;span class=&quot;cm&quot;&gt;/* body x, y point */&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;l&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;						&lt;span class=&quot;cm&quot;&gt;/* body lenth */&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MAXX&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MAXY&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;				&lt;span class=&quot;cm&quot;&gt;/* body array pointer */&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;input&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&#39;6&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;			     	&lt;span class=&quot;cm&quot;&gt;/* default direction */&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;overFlag&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;                 &lt;span class=&quot;cm&quot;&gt;/* judge when the game over, hit wall or eat self*/&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;moveX&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;moveY&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;         &lt;span class=&quot;cm&quot;&gt;/* start move effect */&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;moveFlag&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;                &lt;span class=&quot;cm&quot;&gt;/* restart the loop effection */&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;GetSet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; 
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;请输入游戏空间的宽度：&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;(Please enter the width of game space:)&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;scanf_s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;%d&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;inputX&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;	
	&lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;请输入游戏空间的高度：&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;(Please enter the height of game space:)&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;scanf_s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;%d&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;inputY&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;请输入游戏速度（1 到 n，1 最慢）：&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Please enter the speed of snake moving:(1 is slowest)&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;scanf_s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;%d&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;speed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;InitMap&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;inputY&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;inputX&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

		&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
			&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;inputX&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;inputY&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
			&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
				&lt;span class=&quot;n&quot;&gt;mapArr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
			&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
			&lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;
			&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
				&lt;span class=&quot;n&quot;&gt;mapArr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
			&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
		&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;PrintMap&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;inputY&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;inputX&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
		&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
			&lt;span class=&quot;k&quot;&gt;switch&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mapArr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
			&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
			&lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
				&lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot; &quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
				&lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
			&lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
				&lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;+&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
				&lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
			&lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
				&lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;*&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
				&lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
			&lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
				&lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;@&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
			&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
		&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;StartMsg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;   
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&#39;2(top)&#39;, &#39;8(down)&#39;, &#39;4(left)&#39;, 6(right)&#39; 或 &lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&#39;w(top)&#39;, &#39;a(left)&#39;, &#39;s(down)&#39;, &#39;d(right)&#39;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;控制方向(control the direction)&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;SetRandNum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; 
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;srand&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mapArr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;randX&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;randY&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;foodFlag&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;randX&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rand&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;inputX&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;randY&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rand&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;inputY&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;mapArr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;randX&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;randY&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;	&lt;span class=&quot;cm&quot;&gt;/* set foot number 3 */&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;foodFlag&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;SetSnakeNum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_kbhit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;         &lt;span class=&quot;cm&quot;&gt;/* if there is an input, get it; if not, go on	*/&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_getch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;		
		&lt;span class=&quot;k&quot;&gt;switch&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
		&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&#39;2&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&#39;w&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
			&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&#39;4&#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&#39;6&#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&#39;a&#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&#39;d&#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&#39;2&#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&#39;w&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
				&lt;span class=&quot;n&quot;&gt;input&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
			&lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&#39;8&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&#39;s&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
			&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&#39;4&#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&#39;6&#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&#39;a&#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&#39;d&#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&#39;8&#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&#39;s&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
				&lt;span class=&quot;n&quot;&gt;input&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
			&lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&#39;4&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&#39;a&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
			&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&#39;2&#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&#39;8&#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&#39;w&#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&#39;s&#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&#39;4&#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&#39;a&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
				&lt;span class=&quot;n&quot;&gt;input&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
			&lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&#39;6&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&#39;d&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
			&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&#39;2&#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&#39;8&#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&#39;w&#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&#39;s&#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&#39;6&#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&#39;d&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
				&lt;span class=&quot;n&quot;&gt;input&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
			&lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;switch&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;				&lt;span class=&quot;cm&quot;&gt;/* judge the direction by value of input */&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&#39;2&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;					&lt;span class=&quot;cm&quot;&gt;/* up */&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&#39;w&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;sy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;--&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&#39;8&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;					&lt;span class=&quot;cm&quot;&gt;/* down */&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&#39;s&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;sy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&#39;4&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;					&lt;span class=&quot;cm&quot;&gt;/* left */&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&#39;a&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;sx&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;--&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&#39;6&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;					&lt;span class=&quot;cm&quot;&gt;/* right */&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&#39;d&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;sx&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;--&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;           &lt;span class=&quot;cm&quot;&gt;/*  every point&#39;s address of body move back one point */&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
		&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;                 &lt;span class=&quot;cm&quot;&gt;/* change value by pointer */&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mapArr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;    &lt;span class=&quot;cm&quot;&gt;/* judge when the snake hit the wall or eat itself */&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;overFlag&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;              &lt;span class=&quot;cm&quot;&gt;/* assign the head of snake by pointer */&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;EatFood&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; 
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;foodFlag&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;StartGame&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;sx&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;sy&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;l&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;input&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&#39;6&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;             &lt;span class=&quot;cm&quot;&gt;/* assign the snake body initial address value*/&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mapArr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sx&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;   
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;overFlag&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;    &lt;span class=&quot;cm&quot;&gt;/* loop until the game over */&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;InitMap&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;SetSnakeNum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;SetRandNum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;EatFood&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;PrintMap&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;StartMsg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;Sleep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;speed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;system&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;cls&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;SetMoveNum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;cm&quot;&gt;/* x move 1 -- (X - 2 ); y move 1 -- (Y - 2) */&lt;/span&gt;
	&lt;span class=&quot;cm&quot;&gt;/* move x from left to right */&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;moveY&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;moveFlag&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;moveX&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;inputX&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;moveFlag&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;mapArr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;moveX&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;moveY&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;moveX&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;cm&quot;&gt;/* move y from top to buttom */&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;moveX&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;inputX&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;moveFlag&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;moveY&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;inputY&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;moveFlag&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;mapArr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;moveX&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;moveY&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;moveY&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;cm&quot;&gt;/* move x from right to left */&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;moveY&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;inputY&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;moveFlag&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;moveX&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;moveFlag&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;mapArr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;moveX&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;moveY&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;moveX&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;--&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;cm&quot;&gt;/* move y from buttom to top */&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;moveX&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;moveFlag&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;moveY&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;moveFlag&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;mapArr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;moveX&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;moveY&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;moveY&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;--&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;moveY&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;moveFlag&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;     &lt;span class=&quot;cm&quot;&gt;/* judge when to jump to a deeper layer */&lt;/span&gt;
		&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
			&lt;span class=&quot;n&quot;&gt;moveFlag&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;JudgeEnd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; 
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tmp&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;inputY&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;inputX&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
		&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
			&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mapArr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
				&lt;span class=&quot;k&quot;&gt;goto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;moveX&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;moveY&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;InitMap&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;moveFlag&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;nl&quot;&gt;out:&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;StartView&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;moveX&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;moveY&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;moveFlag&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;startFlag&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;InitMap&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;startFlag&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;SetMoveNum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;PrintMap&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;按任意键开始游戏：&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;(Press any key to start game: )&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;Sleep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;system&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;cls&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;JudgeEnd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_kbhit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
		&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
			&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_getch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
			&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&#39;2&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&#39;w&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&#39;8&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&#39;s&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&#39;4&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&#39;a&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&#39;6&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&#39;d&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
				&lt;span class=&quot;n&quot;&gt;startFlag&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;             &lt;span class=&quot;cm&quot;&gt;/* main function */&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;是否修改设置（修改输入“y”,否则按任意键）：&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Edit the game setting or not ? (Press &#39;y&#39; to edit, or press another key to go on:)&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_getch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&#39;y&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
		&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
			&lt;span class=&quot;n&quot;&gt;GetSet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;          
		&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;StartView&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;  &lt;span class=&quot;cm&quot;&gt;/* an animation before game start */&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;StartGame&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Game Over !!!&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;游戏结束，按任意键继续：&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;(Press any key to restart: )&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;_getch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;overFlag&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;   &lt;span class=&quot;cm&quot;&gt;/* restart the game by the flag */&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;system&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;cls&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;h4 id=&quot;返回顶部&quot;&gt;返回&lt;a href=&quot;#home&quot;&gt;顶部&lt;/a&gt;&lt;/h4&gt;
&lt;p&gt;有更简单的算法欢迎评论指正！&lt;/p&gt;

            </description>
            <pubDate>Tue, 24 Apr 2018 00:00:00 +0000</pubDate>
            <guid>
                https://knightyun.github.io/2018/04/24/c-gluttonous-snake
            </guid>
        </item>
        
        <item>
            <title>Kali Linux之软件安装、卸载、更新和修改更新源</title>
            <link>
                https://knightyun.github.io/2018/04/21/linux-sources-list
            </link>
            <description>
                &lt;p&gt;使用Linux系统，与Windows系统一样，也需要及时进行软件与系统的更新。&lt;/p&gt;

&lt;h4 id=&quot;软件&quot;&gt;软件&lt;/h4&gt;
&lt;p&gt;这里以 &lt;a href=&quot;https://blog.csdn.net/knigh_yun/article/details/79949512&quot;&gt;Kali Linux&lt;/a&gt; 系统为例，介绍常用的软件安装、卸载与更新命令：&lt;/p&gt;

&lt;h6 id=&quot;软件安装&quot;&gt;软件安装&lt;/h6&gt;

&lt;p&gt;安装前先搜索一下更新源中是否有该软件，这里使用 &lt;code class=&quot;highlighter-rouge&quot;&gt;apt&lt;/code&gt; 命令，貌似比另外一个类似的命令 &lt;code class=&quot;highlighter-rouge&quot;&gt;apt-get&lt;/code&gt; 友好一些。&lt;/p&gt;
&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;例如我们安装 &lt;code class=&quot;highlighter-rouge&quot;&gt;leafpad&lt;/code&gt; 这个软件：&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;apt search leafpad
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;然后安装这个软件：&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;apt install leafpad &amp;gt;**然后确定安装就行了。** &amp;gt;**有时会出现一些 `failed` 可以按照提示使用命令 `apt install --fix-missing` 修复。**
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h6 id=&quot;软件卸载&quot;&gt;软件卸载&lt;/h6&gt;
&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;简单卸载软件：&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;apt remove leafpad &amp;gt;**卸载软件并移除配置文件：**

apt-get purge leafpad &amp;gt;**卸载自动安装并且未使用的软件**

apt autoremove
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h6 id=&quot;软件更新&quot;&gt;软件更新&lt;/h6&gt;
&lt;p&gt;先更新一下源：&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;apt update &amp;gt;**这个操作并没有开始更新软件，类似于将远程源中的最新版本信息更新到本地**
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;接下来才开始更新软件：&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;apt upgrade 更新系统：

apt full-upgrade 或者：

apt-get dist-upgrade 清理安装包：

apt-get clean
apt-get autoclean #### 更新源
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Linux 更新源文件位于 &lt;code class=&quot;highlighter-rouge&quot;&gt;/etc/apt/sources.list&lt;/code&gt; ，系统就是从这个文件中读取一些网址参数下载安装软件，默认的是 kali 官方源，我们可以修改为国内一些较快的源，例如阿里、中科大、网易等，加快下载速度。&lt;/p&gt;

&lt;p&gt;找到并编辑上述 &lt;code class=&quot;highlighter-rouge&quot;&gt;sources.list&lt;/code&gt; 源文件，替换为以下内容：&lt;/p&gt;
&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;#aliyun 阿里云
deb http://mirrors.aliyun.com/kali kali-rolling main non-free contrib
deb-src http://mirrors.aliyun.com/kali kali-rolling main non-free contrib

# ustc 中科大
deb http://mirrors.ustc.edu.cn/kali kali-rolling main non-free contrib
deb-src http://mirrors.ustc.edu.cn/kali kali-rolling main non-free contrib
#deb http://mirrors.ustc.edu.cn/kali-security kali-current/updates main contrib non-free
#deb-src http://mirrors.ustc.edu.cn/kali-security kali-current/updates main contrib non-free

# kali 官方源
deb http://http.kali.org/kali kali-rolling main non-free contrib 
deb-src http://http.kali.org/kali kali-rolling main non-free contrib 

# 默认的，可以注释掉不用管
#deb http://security.kali.org/kali-security kali-rolling/updates main contrib non-free
#deb-src http://security.kali.org/kali-security kali-rolling/updates main contrib non-free
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;blockquote&gt;
  &lt;p&gt;以上是目前加快的，也可以百度一下增加其它源&lt;/p&gt;
&lt;/blockquote&gt;

&lt;hr /&gt;

            </description>
            <pubDate>Sat, 21 Apr 2018 00:00:00 +0000</pubDate>
            <guid>
                https://knightyun.github.io/2018/04/21/linux-sources-list
            </guid>
        </item>
        
        <item>
            <title>Linux初识之Kali Linux系统安装详细教程（虚拟机）</title>
            <link>
                https://knightyun.github.io/2018/04/15/kali-linux-install
            </link>
            <description>
                &lt;h3 id=&quot;目录-&quot;&gt;目录 &lt;span id=&quot;home&quot;&gt;&lt;/span&gt;&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#1&quot;&gt;一、Kali Linux 介绍&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#1.1&quot;&gt;1、Linux&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#1.2&quot;&gt;2、Kali&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#2&quot;&gt;二、虚拟机安装与配置&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#2.1&quot;&gt;1、下载&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#2.2&quot;&gt;2、安装配置&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#3&quot;&gt;三、Kali系统安装与配置&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;
&lt;h3 id=&quot;一kali-linux-介绍-&quot;&gt;一、Kali Linux 介绍 &lt;span id=&quot;1&quot;&gt;&lt;/span&gt;&lt;/h3&gt;

&lt;h4 id=&quot;1linux-&quot;&gt;1、Linux &lt;span id=&quot;1.1&quot;&gt;&lt;/span&gt;&lt;/h4&gt;

&lt;p&gt;&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad333eb8df19.jpg&quot; alt=&quot;timg2.jpg&quot; /&gt;
引用一下&lt;a href=&quot;https://baike.baidu.com/item/linux/27050?fr=aladdin&quot;&gt;百度百科&lt;/a&gt;：
Linux是一套免费使用和自由传播的类Unix操作系统，是一个基于POSIX和UNIX的多用户、多任务、支持多线程和多CPU的操作系统。它能运行主要的UNIX工具软件、应用程序和网络协议。它支持32位和64位硬件。Linux继承了Unix以网络为核心的设计思想，是一个性能稳定的多用户网络操作系统。&lt;/p&gt;

&lt;p&gt;Linux有多个发行版本，以下是&lt;a href=&quot;https://www.linux.org/&quot;&gt;官网&lt;/a&gt;提供下载的一些版本：&lt;/p&gt;

&lt;hr /&gt;
&lt;p&gt;&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad33ed9a2cb8.jpg&quot; alt=&quot;20180415140406753.jpg&quot; /&gt;&lt;/p&gt;

&lt;hr /&gt;

&lt;h4 id=&quot;2kali-&quot;&gt;2、Kali &lt;span id=&quot;1.2&quot;&gt;&lt;/span&gt;&lt;/h4&gt;

&lt;p&gt;&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad3332fef322.jpg&quot; alt=&quot;timg.jpg&quot; /&gt;&lt;/p&gt;

&lt;p&gt;kali是linux其中一个发行版，基于Debian，前身是BackTrack（简称BT系统）。kali系统内置大量渗透测试软件，黑客工具箱已不足以形容它，可以说是巨大的渗透系统，涵盖了多个领域，如无线网络、数字取证、服务器、密码、系统漏洞等等，知名软件有：wireshark、aircrack-ng、nmap、hashcat、metasploit-framework(msf)。&lt;/p&gt;

&lt;h3 id=&quot;二虚拟机软安装与配置-&quot;&gt;二、虚拟机软安装与配置 &lt;span id=&quot;2&quot;&gt;&lt;/span&gt;&lt;/h3&gt;

&lt;h4 id=&quot;1下载-&quot;&gt;1、下载 &lt;span id=&quot;2.1&quot;&gt;&lt;/span&gt;&lt;/h4&gt;
&lt;p&gt;这里将详细介绍在Windows虚拟机中安装kali linux，使用的虚拟机软件是 &lt;strong&gt;VMware&lt;/strong&gt;，可以去&lt;a href=&quot;https://www.vmware.com/products/workstation-pro/workstation-pro-evaluation.html&quot;&gt;官网下载&lt;/a&gt;最新版本，&lt;strong&gt;注意：&lt;/strong&gt; VMware 10.0版本之后只支持&lt;strong&gt;64位&lt;/strong&gt;系统，如果是32位系统用户需要下载10.0及之前的版本。
然后下载发型版的kali linux，&lt;a href=&quot;https://www.kali.org/downloads/&quot;&gt;官网下载&lt;/a&gt;，根据自己情况选择下载32位或64位，完整版或者轻便版，这里我下载的64位完整版。&lt;/p&gt;

&lt;h4 id=&quot;1安装配置-&quot;&gt;1、安装配置 &lt;span id=&quot;2.2&quot;&gt;&lt;/span&gt;&lt;/h4&gt;
&lt;p&gt;接下来一步步安装：
&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad33ed9b4f7a.jpg&quot; alt=&quot;20180415144118372.jpg&quot; /&gt;
选择安装位置，注意保证空间充足：
&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad33ed9b13c4.jpg&quot; alt=&quot;20180415144303213.jpg&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad33ed9afe91.jpg&quot; alt=&quot;20180415144404353.jpg&quot; /&gt;&lt;/p&gt;

&lt;p&gt;等待安装完成：
&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad33ed9b279d.jpg&quot; alt=&quot;20180415144453641.jpg&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad33ed9b7ebf.jpg&quot; alt=&quot;20180415144527264.jpg&quot; /&gt;&lt;/p&gt;

&lt;p&gt;这是试用版本，需要购买密钥，鼓励购买正版，蛮穷的可以使用以下任一密钥：&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;FF31K-AHZD1-H8ETZ-8WWEZ-WUUVA&lt;/p&gt;
  &lt;blockquote&gt;
    &lt;p&gt;CV7T2-6WY5Q-48EWP-ZXY7X-QGUWD&lt;/p&gt;
  &lt;/blockquote&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad33ed9ae9ca.jpg&quot; alt=&quot;20180415144552593.jpg&quot; /&gt;&lt;/p&gt;

&lt;p&gt;安装完成，然后新建虚拟机：
&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad33ed9b66b3.jpg&quot; alt=&quot;20180415144927247.jpg&quot; /&gt;&lt;/p&gt;

&lt;p&gt;选择推荐的即可：
&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad33ed9b3abd.jpg&quot; alt=&quot;20180415145002833.jpg&quot; /&gt;&lt;/p&gt;

&lt;p&gt;这里选择安装程序映像文件，浏览选择刚才下载的kali linux文件，后缀是 &lt;code class=&quot;highlighter-rouge&quot;&gt;.iso&lt;/code&gt;：
&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad33ed9b9401.jpg&quot; alt=&quot;20180415145126392.jpg&quot; /&gt;&lt;/p&gt;

&lt;p&gt;选择操作系统“linux”，版本是“Debian”：
&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad33f949f756.jpg&quot; alt=&quot;20180415145812273.jpg&quot; /&gt;&lt;/p&gt;

&lt;p&gt;名称随便取，然后选择虚拟机文件存放位置：&lt;/p&gt;
&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;注意选择合适位置，这里需要存放几十G的虚拟磁盘文件&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad33f94a5c83.jpg&quot; alt=&quot;20180415145936586.jpg&quot; /&gt;&lt;/p&gt;

&lt;p&gt;设置虚拟磁盘的大小，一般不能低于默认值，下面一般选择单个文件，方便：
&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad33f94b523f.jpg&quot; alt=&quot;20180415150112131.jpg&quot; /&gt;&lt;/p&gt;

&lt;p&gt;新建完成，硬件可以之后自定义：
&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad33f94b3b28.jpg&quot; alt=&quot;20180415150311408.jpg&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad33f94e3f6e.jpg&quot; alt=&quot;20180415150339668.jpg&quot; /&gt;&lt;/p&gt;

&lt;p&gt;觉得默认配置不合适可以自定义调整，例如修改内存大小，增加磁盘：
&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad33f9503b03.jpg&quot; alt=&quot;20180415150430194.jpg&quot; /&gt;&lt;/p&gt;

&lt;p&gt;设置网络适配器模式，可以桥接物理网络（例如电脑连接wifi，虚拟机就和电脑连接同一个wifi），或者NAT模式（类似于电脑成为一个路由器，虚拟机连接电脑的热点），如果不想联网，只用于物理机和虚拟机之间的交流，可以选择主机模式：
&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad33f94f1e46.jpg&quot; alt=&quot;20180415150747931.jpg&quot; /&gt;&lt;/p&gt;

&lt;p&gt;虚拟机配置完成，接下来开始安装 kali 系统。&lt;/p&gt;

&lt;h3 id=&quot;三kali系统安装与配置-&quot;&gt;三、Kali系统安装与配置 &lt;span id=&quot;3&quot;&gt;&lt;/span&gt;&lt;/h3&gt;

&lt;p&gt;点击启动虚拟机：
&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad33f94edca2.jpg&quot; alt=&quot;20180415152337260.jpg&quot; /&gt;&lt;/p&gt;

&lt;p&gt;鼠标点击安装界面，之后用键盘方向键和Enter键操作：&lt;/p&gt;
&lt;blockquote&gt;
  &lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;Live&lt;/code&gt; 开头的不是系统安装，类似于Windows PE，用于恢复系统，其他选项可以不用管，也暂时用不着。
&lt;strong&gt;这里我们选择简单的图形化安装&lt;/strong&gt; &lt;code class=&quot;highlighter-rouge&quot;&gt;Graphical install&lt;/code&gt; 
此时鼠标无法操作，退出虚拟机操作界面使用 &lt;code class=&quot;highlighter-rouge&quot;&gt;ctrl + alt&lt;/code&gt; 键。&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad33f94d2cb9.jpg&quot; alt=&quot;2018041515321839.jpg&quot; /&gt;&lt;/p&gt;

&lt;p&gt;选择简体中文：
&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad33f94eae89.jpg&quot; alt=&quot;20180415153852638.jpg&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad3403ff0501.jpg&quot; alt=&quot;20180415153941517.jpg&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad340400b3b8.jpg&quot; alt=&quot;20180415154023144.jpg&quot; /&gt;&lt;/p&gt;

&lt;p&gt;等待配置一会：
&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad3404025f68.jpg&quot; alt=&quot;20180415154326948.jpg&quot; /&gt;&lt;/p&gt;

&lt;p&gt;设置主机名称，和Windows的主机名一样：
&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad3404034393.jpg&quot; alt=&quot;20180415154445426.jpg&quot; /&gt;&lt;/p&gt;

&lt;p&gt;可以暂时不用输入：
&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad34040359fa.jpg&quot; alt=&quot;2018041515470772.jpg&quot; /&gt;&lt;/p&gt;

&lt;p&gt;设置用户密码登录：
&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad340404446a.jpg&quot; alt=&quot;20180415154826476.jpg&quot; /&gt;&lt;/p&gt;

&lt;p&gt;等待配置：
&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad34040310fe.jpg&quot; alt=&quot;20180415154926766.jpg&quot; /&gt;&lt;/p&gt;

&lt;p&gt;可以选择使用整个磁盘：
&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad3404040f96.jpg&quot; alt=&quot;20180415155254743.jpg&quot; /&gt;&lt;/p&gt;

&lt;hr /&gt;
&lt;p&gt;&lt;strong&gt;注：&lt;/strong&gt;
这里简单介绍一下里面的 &lt;code class=&quot;highlighter-rouge&quot;&gt;LVM&lt;/code&gt; 选项：
详见&lt;a href=&quot;https://baike.baidu.com/item/LVM/6571177?fr=aladdin&quot;&gt;百度百科&lt;/a&gt;，LVM是 Logical Volume Manager（逻辑卷管理）的简写，它是Linux环境下对磁盘分区进行管理的一种机制。安装Linux时常出现的一个问题就是合理分区，根据使用情况设置 &lt;code class=&quot;highlighter-rouge&quot;&gt;/boot&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;/var&lt;/code&gt;,  &lt;code class=&quot;highlighter-rouge&quot;&gt;/home&lt;/code&gt; 等区块的大小，设置好之后再想要改变就很麻烦，要用分区工具压缩一部分的空闲区出去，然后合并到空间不足的区域。&lt;/p&gt;

&lt;p&gt;个人对LVM的理解是，它类似于使用一种&lt;strong&gt;文件夹&lt;/strong&gt;的机制，直接使用整块磁盘，把每个分区设置成一种类似文件夹的存在，因为文件夹不会限制大小，因此就能动态调整各区的大小，方便管理。&lt;/p&gt;

&lt;hr /&gt;
&lt;p&gt;继续：
&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad340403f668.jpg&quot; alt=&quot;20180415160939891.jpg&quot; /&gt;&lt;/p&gt;

&lt;p&gt;可以使用推荐的：
&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad34040429bb.jpg&quot; alt=&quot;201804151610212.jpg&quot; /&gt;&lt;/p&gt;

&lt;p&gt;确定分配，或者更改区块大小和新增区块：
&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad3432baa37c.jpg&quot; alt=&quot;20180415161246873.jpg&quot; /&gt;&lt;/p&gt;

&lt;p&gt;确认写入：
&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad3432b9fe9b.jpg&quot; alt=&quot;20180415161357309.jpg&quot; /&gt;&lt;/p&gt;

&lt;p&gt;接下来安装时间有点长，喝杯茶休息一下 -_- 
&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad3432b8d7ed.jpg&quot; alt=&quot;20180415161452740.jpg&quot; /&gt;&lt;/p&gt;

&lt;p&gt;终于安装完成，半小时安静的过去了……
这里选择不使用网络镜像：
&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad3432b91756.jpg&quot; alt=&quot;20180415164804954.jpg&quot; /&gt;&lt;/p&gt;

&lt;p&gt;配置GRUB：
&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad3432ba8703.jpg&quot; alt=&quot;20180415165316774.jpg&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad3432ba4ff0.jpg&quot; alt=&quot;20180415165336903.jpg&quot; /&gt;&lt;/p&gt;

&lt;p&gt;安装完成：
&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad343e7d33a7.jpg&quot; alt=&quot;20180415165446531.jpg&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad343e7cbddf.jpg&quot; alt=&quot;20180415165502624.jpg&quot; /&gt;&lt;/p&gt;

&lt;p&gt;选择启动，第二个选项是恢复模式：
&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad343e7d9d87.jpg&quot; alt=&quot;20180415175203402.jpg&quot; /&gt;&lt;/p&gt;

&lt;p&gt;初始用户名是 &lt;strong&gt;root&lt;/strong&gt;：
&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad343e7d0471.jpg&quot; alt=&quot;2018041517561331.jpg&quot; /&gt;&lt;/p&gt;

&lt;p&gt;之前设置的密码：
&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad343e7d1c6b.jpg&quot; alt=&quot;20180415185003860.jpg&quot; /&gt;&lt;/p&gt;

&lt;p&gt;到此kali系统就安装完毕了：
&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad343e7e0f5c.jpg&quot; alt=&quot;20180415185053453.jpg&quot; /&gt;&lt;/p&gt;

&lt;p&gt;内置大量软件，更多功能可以自行发掘：
&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad343e7f2511.jpg&quot; alt=&quot;20180415185744699.jpg&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad343e7ef4bf.jpg&quot; alt=&quot;20180415185813698.jpg&quot; /&gt;&lt;/p&gt;

&lt;h3 id=&quot;返回顶部&quot;&gt;返回&lt;a href=&quot;#home&quot;&gt;顶部&lt;/a&gt;&lt;/h3&gt;

&lt;hr /&gt;

            </description>
            <pubDate>Sun, 15 Apr 2018 00:00:00 +0000</pubDate>
            <guid>
                https://knightyun.github.io/2018/04/15/kali-linux-install
            </guid>
        </item>
        
        <item>
            <title>C语言自增自减运算辨析</title>
            <link>
                https://knightyun.github.io/2018/04/10/c-self-plus
            </link>
            <description>
                &lt;p&gt;c语言中，自增(++)和自减(–)运算是很有c语言“感觉”的一种运算符，但是在实际编写中，尤其对初学者或者很久没接触它的，会对它的原理和运算结果产生混淆，接下来做详细辨析。&lt;/p&gt;

&lt;h4 id=&quot;自增运算&quot;&gt;自增运算（++）&lt;/h4&gt;

&lt;p&gt;语法为：&lt;code class=&quot;highlighter-rouge&quot;&gt;a++&lt;/code&gt;，其结果与：&lt;code class=&quot;highlighter-rouge&quot;&gt;a = a + 1&lt;/code&gt; 一样，也和：&lt;code class=&quot;highlighter-rouge&quot;&gt;a += 1&lt;/code&gt; 一样，作用很明显，方便阅读，减小代码量。例如下面的例子，就能看出明显的效果了：&lt;/p&gt;
&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sqrt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sqrt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;cm&quot;&gt;/*简化后*/&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sqrt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h5 id=&quot;辨析&quot;&gt;辨析&lt;/h5&gt;

&lt;p&gt;自增也可以表示为 &lt;code class=&quot;highlighter-rouge&quot;&gt;++a&lt;/code&gt;， &lt;strong&gt;它们都能把a 的值加 1&lt;/strong&gt;，但是两个&lt;strong&gt;表达式的值&lt;/strong&gt;却不同，用一个例子说明：&lt;/p&gt;
&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cp&quot;&gt;#include&amp;lt;stdio.h&amp;gt;
&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;a2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;b2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;a = %d, b = %d, a2 = %d, b2 = %d&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;输出结果为：&lt;/p&gt;
&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;a = 2, b = 2, a2 = 1, b2 = 2
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;其实，&lt;code class=&quot;highlighter-rouge&quot;&gt;a++&lt;/code&gt; 的值为 &lt;code class=&quot;highlighter-rouge&quot;&gt;a&lt;/code&gt; 本身，&lt;code class=&quot;highlighter-rouge&quot;&gt;++a&lt;/code&gt; 的值为 &lt;code class=&quot;highlighter-rouge&quot;&gt;a+1&lt;/code&gt; 后的值。&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;也可以按教科书那种记：&lt;/strong&gt;&lt;/p&gt;
  &lt;blockquote&gt;
    &lt;p&gt;&lt;strong&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;b = a++&lt;/code&gt; 是先赋值后运算，即先 &lt;code class=&quot;highlighter-rouge&quot;&gt;b=a&lt;/code&gt;，然后 &lt;code class=&quot;highlighter-rouge&quot;&gt;a++&lt;/code&gt;。&lt;/strong&gt;
&lt;strong&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;b = ++a&lt;/code&gt; 是先运算后赋值，即先 &lt;code class=&quot;highlighter-rouge&quot;&gt;++a&lt;/code&gt;，然后 &lt;code class=&quot;highlighter-rouge&quot;&gt;++a&lt;/code&gt;。&lt;/strong&gt;&lt;/p&gt;
  &lt;/blockquote&gt;
&lt;/blockquote&gt;

&lt;h4 id=&quot;自减运算&quot;&gt;自减运算&lt;/h4&gt;

&lt;p&gt;自减运算原理和辨析与上面自增运算相似，&lt;code class=&quot;highlighter-rouge&quot;&gt;a--&lt;/code&gt; 等于 &lt;code class=&quot;highlighter-rouge&quot;&gt;a = a -1&lt;/code&gt; ，表达式 &lt;code class=&quot;highlighter-rouge&quot;&gt;a--&lt;/code&gt; 的值为 &lt;code class=&quot;highlighter-rouge&quot;&gt;a&lt;/code&gt; ，表达式 &lt;code class=&quot;highlighter-rouge&quot;&gt;--a&lt;/code&gt; 的值为 &lt;code class=&quot;highlighter-rouge&quot;&gt;a-1&lt;/code&gt;。&lt;/p&gt;

            </description>
            <pubDate>Tue, 10 Apr 2018 00:00:00 +0000</pubDate>
            <guid>
                https://knightyun.github.io/2018/04/10/c-self-plus
            </guid>
        </item>
        
        <item>
            <title>递归函数之阶乘的实现</title>
            <link>
                https://knightyun.github.io/2018/04/06/recursion-factorial
            </link>
            <description>
                &lt;h4 id=&quot;定义&quot;&gt;定义&lt;/h4&gt;
&lt;p&gt;在编程中函数有一个神奇又难理解的功能，就是递归。递归就是在一个过程中要调用上一步或上几步的结果，使用递归过程的函数就叫递归函数。简单说就是函数自身调用自身（听着有点反自然，像自己举起自己）。&lt;/p&gt;

&lt;h4 id=&quot;递归实例&quot;&gt;递归实例&lt;/h4&gt;

&lt;p&gt;除了数学的复杂运算中，生活中也有不少递归的实例：&lt;/p&gt;

&lt;h5 id=&quot;德罗斯特效应&quot;&gt;德罗斯特效应&lt;/h5&gt;
&lt;p&gt;德罗斯特效应（Droste effect）是递归的一种视觉形式，即一张图片中的某部分与整张图片相同，如下：
&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad3565ad7935.jpg&quot; alt=&quot;20180406205325252.jpg&quot; /&gt;&lt;/p&gt;

&lt;hr /&gt;
&lt;p&gt;&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad3565b03bca.jpg&quot; alt=&quot;20180406205355939.jpg&quot; /&gt;&lt;/p&gt;

&lt;hr /&gt;
&lt;p&gt;&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad3565b0245f.jpg&quot; alt=&quot;20180406205418499.jpg&quot; /&gt;&lt;/p&gt;

&lt;hr /&gt;
&lt;blockquote&gt;
  &lt;p&gt;别晕、别晕 -_-&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;或者在自己身前和身后都放一面镜子，深刻体会一会儿 ▄︻┻┳══━一&lt;/p&gt;

&lt;h4 id=&quot;递归实现&quot;&gt;递归实现&lt;/h4&gt;

&lt;p&gt;接下来就用C语言递归函数来实现阶乘功能，源码如下：&lt;/p&gt;
&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cp&quot;&gt;#include &amp;lt;stdio.h&amp;gt;
&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;factorial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;cm&quot;&gt;/*外部定义阶乘函数*/&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;cm&quot;&gt;/*注意要给函数设置一个递归结束条件*/&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;输入错误！&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; 
		&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;	
	&lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;factorial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;cm&quot;&gt;/*调用函数factorial()自身*/&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; 
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;%d&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;factorial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt; 
	
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;这是程序大概的运算过程：
&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad3565b00bba.jpg&quot; alt=&quot;20180406220517224.jpg&quot; /&gt;&lt;/p&gt;

&lt;hr /&gt;
&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;这里要注意给函数设置一个递归结束条件，可以是 if 判断句，不然函数就无限调用自身下去了，你之前看着镜子中的镜子中的……自己都晕，程序自然是崩溃了&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

            </description>
            <pubDate>Fri, 06 Apr 2018 00:00:00 +0000</pubDate>
            <guid>
                https://knightyun.github.io/2018/04/06/recursion-factorial
            </guid>
        </item>
        
        <item>
            <title>位运算符与位运算</title>
            <link>
                https://knightyun.github.io/2018/04/06/bit-operation
            </link>
            <description>
                &lt;h3 id=&quot;目录-&quot;&gt;目录 &lt;span id=&quot;home&quot;&gt;&lt;/span&gt;&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#1&quot;&gt;概述&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#2&quot;&gt;按位与&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#3&quot;&gt;按位或&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#4&quot;&gt;按位异或&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#4.1&quot;&gt;简单应用&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#4.1.1&quot;&gt;交换变量值&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#4.1.2&quot;&gt;简单加密&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#6&quot;&gt;移位运算&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#6.1&quot;&gt;左移位&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#6.2&quot;&gt;右移位&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#7&quot;&gt;复合赋值符&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;一概述-&quot;&gt;一、概述 &lt;span id=&quot;1&quot;&gt;&lt;/span&gt;&lt;/h3&gt;
&lt;p&gt;程序中的所有数在计算机内存中都是以二进制的形式储存的。除了常见的&lt;strong&gt;算术运算符&lt;/strong&gt;：&lt;code class=&quot;highlighter-rouge&quot;&gt;+ - * / %&lt;/code&gt;，还有&lt;strong&gt;位运算&lt;/strong&gt;：&lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;amp; | ^ ~ &amp;gt;&amp;gt; &amp;lt;&amp;lt;&lt;/code&gt;，就是直接对整数在内存中的二进制位进行操作。接下来以C语言为例介绍，其它语言大同小异。&lt;/p&gt;

&lt;h3 id=&quot;二按位与&quot;&gt;二、按位与（&amp;amp;）&lt;span id=&quot;2&quot;&gt;&lt;/span&gt;&lt;/h3&gt;
&lt;p&gt;又叫 &lt;code class=&quot;highlighter-rouge&quot;&gt;and&lt;/code&gt; 运算，用符号 &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;amp;&lt;/code&gt; 表示，计算方式如下：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;1 &amp;amp; 1 = 1&lt;/code&gt; , &lt;code class=&quot;highlighter-rouge&quot;&gt;0 &amp;amp; 1 = 0&lt;/code&gt; , ` 0 &amp;amp; 0 = 0`&lt;/p&gt;

&lt;p&gt;布尔类型中，&lt;code class=&quot;highlighter-rouge&quot;&gt;1&lt;/code&gt; 表示真，&lt;code class=&quot;highlighter-rouge&quot;&gt;0&lt;/code&gt; 表示假，所以可以用高中数学那句话来记：&lt;strong&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;同真为真，一假为假&lt;/code&gt;&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;举个例子：&lt;code class=&quot;highlighter-rouge&quot;&gt;3 &amp;amp; 5 = 1&lt;/code&gt; ，运算过程如下：&lt;/p&gt;
&lt;pre&gt;
   0 1 1   ---&amp;gt; 3
 &amp;amp; 1 0 1   ---&amp;gt; 5
  --------
   0 0 1   ---&amp;gt; 1
&lt;/pre&gt;

&lt;h3 id=&quot;三按位或-&quot;&gt;三、按位或 （|）&lt;span id=&quot;3&quot;&gt;&lt;/span&gt;&lt;/h3&gt;
&lt;p&gt;又叫 &lt;code class=&quot;highlighter-rouge&quot;&gt;or&lt;/code&gt; 运算，用符号 &lt;code class=&quot;highlighter-rouge&quot;&gt;|&lt;/code&gt; 表示，运算方式：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;1 | 1 = 1&lt;/code&gt; , &lt;code class=&quot;highlighter-rouge&quot;&gt;1 | 0 = 1&lt;/code&gt; , &lt;code class=&quot;highlighter-rouge&quot;&gt;0 | 0 = 0&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;记为：&lt;strong&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;一真为真，同假为假&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;举例：&lt;code class=&quot;highlighter-rouge&quot;&gt;3 | 5 = 7&lt;/code&gt; , 运算过程：&lt;/p&gt;
&lt;pre&gt;
   0 1 1   ---&amp;gt; 3
 | 1 0 1   ---&amp;gt; 5
 --------
   1 1 1   ---&amp;gt; 7
&lt;/pre&gt;

&lt;h3 id=&quot;四按位异或&quot;&gt;四、按位异或（^）&lt;span id=&quot;4&quot;&gt;&lt;/span&gt;&lt;/h3&gt;

&lt;p&gt;又叫 &lt;code class=&quot;highlighter-rouge&quot;&gt;xor&lt;/code&gt; 运算，用符号 &lt;code class=&quot;highlighter-rouge&quot;&gt;^&lt;/code&gt; 表示，注意这里不是数学表达里面的&lt;strong&gt;次方&lt;/strong&gt;的意思，运算方式：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;1 ^ 1 = 0&lt;/code&gt; , ` 1 ^ 0 = 1&lt;code class=&quot;highlighter-rouge&quot;&gt; , &lt;/code&gt;0 ^ 0 = 0`&lt;/p&gt;

&lt;p&gt;这种运算也较为特殊，记为：&lt;strong&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;同为假，异为真&lt;/code&gt;&lt;/strong&gt;
举例：&lt;code class=&quot;highlighter-rouge&quot;&gt;3 ^ 5 = 6&lt;/code&gt; , 运算过程：&lt;/p&gt;
&lt;pre&gt;
   0 1 1    ---&amp;gt; 3
 ^ 1 0 1    ---&amp;gt; 5
 ---------
   1 1 0    ---&amp;gt; 6
&lt;/pre&gt;

&lt;h4 id=&quot;简单应用-&quot;&gt;简单应用 &lt;span id=&quot;4.1&quot;&gt;&lt;/span&gt;&lt;/h4&gt;

&lt;h5 id=&quot;1交换变量值-&quot;&gt;1、交换变量值 &lt;span id=&quot;4.1.1&quot;&gt;&lt;/span&gt;&lt;/h5&gt;

&lt;p&gt;异或运算有如下特性：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;a ^ b ^a = b&lt;/code&gt; , &lt;code class=&quot;highlighter-rouge&quot;&gt;a ^ b ^ b =a&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;因此可以用于程序中的变量值交换，C语言中，我们可能经常这样交换变量值：&lt;/p&gt;
&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cp&quot;&gt;#include &amp;lt;stdio.h&amp;gt;
&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;temp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;cm&quot;&gt;/*定义一个临时变量用于交换方便*/&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;temp&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;temp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;a = %d, b = %d&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;cm&quot;&gt;/*输出结果为：a = 5, b = 3 实现了变量值的交换*/&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;但是以后我们可以这样实现：&lt;/p&gt;
&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cp&quot;&gt;#include &amp;lt;stdio.h&amp;gt;
&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;^=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;cm&quot;&gt;/*等同于：a = a ^ b 之后会讲*/&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;^&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;cm&quot;&gt;/*这时 a ^ b 等于 原来 a 的值*/&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;^=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;cm&quot;&gt;/*a = a ^ b*/&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;a = %d, b = %d&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;a, b 为负数时同样适用，但位运算只适用于整型，浮点数不可用&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h5 id=&quot;2简单加密-&quot;&gt;2、简单加密 &lt;span id=&quot;4.1.2&quot;&gt;&lt;/span&gt;&lt;/h5&gt;

&lt;p&gt;例如，你想传输一条信息给Ta，信息为：&lt;code class=&quot;highlighter-rouge&quot;&gt;5201314&lt;/code&gt; ，密码为：&lt;code class=&quot;highlighter-rouge&quot;&gt;1998&lt;/code&gt; ，使用如下代码加密：&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;5201314 ^ 1998
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;结果为 &lt;code class=&quot;highlighter-rouge&quot;&gt;5200492&lt;/code&gt;，想要查看原信息，使用密码查看：&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;5200492 ^ 1998 这样就生成了原数字：`5201314`.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;五按位取反&quot;&gt;五、按位取反（~）&lt;span id=&quot;5&quot;&gt;&lt;/span&gt;&lt;/h3&gt;
&lt;p&gt;又叫 &lt;code class=&quot;highlighter-rouge&quot;&gt;not&lt;/code&gt; 运算，即将二进制位中 &lt;code class=&quot;highlighter-rouge&quot;&gt;0&lt;/code&gt; 和 &lt;code class=&quot;highlighter-rouge&quot;&gt;1&lt;/code&gt; 全部取反，运算方式为：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;~ 1 = 0&lt;/code&gt; , &lt;code class=&quot;highlighter-rouge&quot;&gt;~ 0 = 1&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;记为：&lt;strong&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;0 变 1，1 变 0&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;举例：&lt;code class=&quot;highlighter-rouge&quot;&gt;~5 = -6&lt;/code&gt;, 运算过程：&lt;/p&gt;
&lt;pre&gt;
 ~ 00000000 00000000 00000000 00000101   ---&amp;gt; 5
 ---------------------------------------
   11111111 11111111 11111111 11111010   ---&amp;gt; -6
&lt;/pre&gt;
&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;注意变量如果定义是无符号短整型 &lt;code class=&quot;highlighter-rouge&quot;&gt;unsigned short&lt;/code&gt; ， &lt;code class=&quot;highlighter-rouge&quot;&gt;~5&lt;/code&gt; 将不再是 &lt;code class=&quot;highlighter-rouge&quot;&gt;-6&lt;/code&gt; ，而是 &lt;code class=&quot;highlighter-rouge&quot;&gt;65530&lt;/code&gt;&lt;/strong&gt;。
&lt;strong&gt;还有一个规律是正整数取反后结果是原数加一后取相反数，负数一样&lt;/strong&gt;。
&lt;strong&gt;还要注意不要和逻辑运算符 &lt;code class=&quot;highlighter-rouge&quot;&gt;!&lt;/code&gt; 搞混，&lt;code class=&quot;highlighter-rouge&quot;&gt;! 1 = 0&lt;/code&gt; , &lt;code class=&quot;highlighter-rouge&quot;&gt;! 1234 = 0&lt;/code&gt; , &lt;code class=&quot;highlighter-rouge&quot;&gt;! 0 = 1&lt;/code&gt;&lt;/strong&gt;，即只有 &lt;code class=&quot;highlighter-rouge&quot;&gt;真(1)&lt;/code&gt; 与&lt;code class=&quot;highlighter-rouge&quot;&gt;假(0)&lt;/code&gt; 两种。&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3 id=&quot;六移位运算-&quot;&gt;六、移位运算 &lt;span id=&quot;6&quot;&gt;&lt;/span&gt;&lt;/h3&gt;
&lt;p&gt;顾名思义，移位即将数据的二进制数进行向左或向右平移一定的位数，然后得到新的数据，移位分为左移位和右移位。&lt;/p&gt;
&lt;h4 id=&quot;1左移位&quot;&gt;1、左移位（«）&lt;span id=&quot;6.1&quot;&gt;&lt;/span&gt;&lt;/h4&gt;
&lt;p&gt;将数据的转换为二进制，所有位向左平移，高位（左端）舍弃，低位（右端）空位补 &lt;code class=&quot;highlighter-rouge&quot;&gt;0&lt;/code&gt;，格式为：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;需要移位的数据 &amp;lt;&amp;lt; 需要移动的位数&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;举例：&lt;code class=&quot;highlighter-rouge&quot;&gt;5 &amp;lt;&amp;lt; 2 =20&lt;/code&gt; ，运算过程：&lt;/p&gt;

&lt;pre&gt;
  00000000 00000000 00000000 00000101   ---&amp;gt; 5
 -------------------------------------
  00000000 00000000 00000000 00010100   ---&amp;gt; 20
&lt;/pre&gt;
&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;右移位的数学意义是，原数左移 n 位，相当于原数乘以 2 的 n 次方&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4 id=&quot;2右移位-&quot;&gt;2、右移位 &lt;span id=&quot;6.2&quot;&gt;&lt;/span&gt;&lt;/h4&gt;
&lt;p&gt;将数据的二进制所有位向右移位，低位舍弃，高位补 &lt;code class=&quot;highlighter-rouge&quot;&gt;0&lt;/code&gt;（负数补 &lt;code class=&quot;highlighter-rouge&quot;&gt;1&lt;/code&gt;），格式：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;需要移位的数据 &amp;gt;&amp;gt; 需要移动的位数&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;举例：&lt;code class=&quot;highlighter-rouge&quot;&gt;5 &amp;gt;&amp;gt; 2 = 1&lt;/code&gt;，运算过程：&lt;/p&gt;
&lt;pre&gt;
  00000000 00000000 00000000 00000101   ---&amp;gt; 5
 -------------------------------------
  00000000 00000000 00000000 00000001   ---&amp;gt;  1
&lt;/pre&gt;
&lt;p&gt;负数情况：&lt;code class=&quot;highlighter-rouge&quot;&gt;-6 &amp;gt;&amp;gt; 2 = -2&lt;/code&gt;，运算过程：&lt;/p&gt;
&lt;pre&gt;
  11111111 11111111 11111111 11111010    ---&amp;gt; -6
 -------------------------------------
  11111111 11111111 11111111 11111110    ---&amp;gt; -2
&lt;/pre&gt;
&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;右移位的数学意义是，相当于原数除以 2 的 n 次方&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3 id=&quot;七复合赋值符-&quot;&gt;七、复合赋值符 &lt;span id=&quot;7&quot;&gt;&lt;/span&gt;&lt;/h3&gt;

&lt;p&gt;算术运算中有复合赋值符：&lt;code class=&quot;highlighter-rouge&quot;&gt;+= -+ *= /= %=&lt;/code&gt;，位运算也有对应的复合赋值：&lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;amp;= |= ^= &amp;gt;&amp;gt;= &amp;lt;&amp;lt;= &lt;/code&gt;（注意没有 &lt;code class=&quot;highlighter-rouge&quot;&gt;~=&lt;/code&gt; ），运算一样：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;a &amp;amp;= b&lt;/code&gt; 等价于 &lt;code class=&quot;highlighter-rouge&quot;&gt;a = a &amp;amp; b&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;以此类推&lt;/p&gt;

&lt;hr /&gt;
&lt;h3 id=&quot;返回顶部&quot;&gt;返回&lt;a href=&quot;#home&quot;&gt;顶部&lt;/a&gt;&lt;/h3&gt;

            </description>
            <pubDate>Fri, 06 Apr 2018 00:00:00 +0000</pubDate>
            <guid>
                https://knightyun.github.io/2018/04/06/bit-operation
            </guid>
        </item>
        
        <item>
            <title>十进制负数的二进制表示法</title>
            <link>
                https://knightyun.github.io/2018/04/04/negative-binary
            </link>
            <description>
                &lt;p&gt;十进制转正整数转二进制应该都会，用C语言代码简单表示算法：&lt;/p&gt;
&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cp&quot;&gt;#include&amp;lt;stdio.h&amp;gt;
&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dec&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;65535&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;cm&quot;&gt;/*positive decimalism number to binary number*/&lt;/span&gt;
	&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
	&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dec&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;bin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dec&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;dec&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;bin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dec&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;--&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;cm&quot;&gt;/*print binary number*/&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;%d&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]);&lt;/span&gt; 
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;当然也有另外一种简单转换方法：&lt;/p&gt;
&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cp&quot;&gt;#include&amp;lt;stdio.h&amp;gt;
&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dec&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;65535&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;itoa&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dec&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;cm&quot;&gt;/*整型转换为字符串的函数，第一个参数是整型变量，
	第二个是字符数组，用于存放字符串，第三个是进制，“2” 表示输出的字符串的进制格式，
	可以这样记函数：“int to arr” */&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;%s&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;但是，十进制负整数转换为二进制稍微复杂一些，分为三步：&lt;/p&gt;
&lt;h3 id=&quot;一原码&quot;&gt;一、原码&lt;/h3&gt;
&lt;p&gt;例如一个十进制数 5，二进制原码表示为：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;00000000 00000000 00000000 00000101&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;大小为 4 字节，每一个 &lt;code class=&quot;highlighter-rouge&quot;&gt;0&lt;/code&gt; 或  &lt;code class=&quot;highlighter-rouge&quot;&gt;1&lt;/code&gt; 表示一个比特位(bit)，所以八位为一字节，好像32位和64位系统都这样。一字节用十进制整数表示大小则是：2的8次方（256）。
表示为十六进制是这样：&lt;code class=&quot;highlighter-rouge&quot;&gt;ff ff ff ff&lt;/code&gt;（f=2^4-1）&lt;/p&gt;
&lt;h3 id=&quot;二反码&quot;&gt;二、反码&lt;/h3&gt;
&lt;p&gt;将二进制原码每一位取反，就是 &lt;code class=&quot;highlighter-rouge&quot;&gt;0&lt;/code&gt; 变 &lt;code class=&quot;highlighter-rouge&quot;&gt;1&lt;/code&gt; ，&lt;code class=&quot;highlighter-rouge&quot;&gt;1&lt;/code&gt; 变 &lt;code class=&quot;highlighter-rouge&quot;&gt;0&lt;/code&gt;。
上面 5 的反码表示为这样：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;11111111 11111111 11111111 11111010&lt;/code&gt;&lt;/p&gt;
&lt;h3 id=&quot;三补码&quot;&gt;三、补码&lt;/h3&gt;
&lt;p&gt;将反码最低位加 &lt;code class=&quot;highlighter-rouge&quot;&gt;1&lt;/code&gt; 叫做补码，那么 5 的补码表示为：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;11111111 11111111 11111111 11111011&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;注意反码末位是 &lt;code class=&quot;highlighter-rouge&quot;&gt;1&lt;/code&gt; 时记得进位。&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;所以十进制数 &lt;code class=&quot;highlighter-rouge&quot;&gt;-5&lt;/code&gt; 的二进制表示为：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;11111111 11111111 11111111 11111011&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;所以 -1 在计算机中表示为全 &lt;code class=&quot;highlighter-rouge&quot;&gt;1&lt;/code&gt;，就是：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;11111111 11111111 11111111 11111111&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;我64位计算机中是这样的：&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad3575f46acf.jpg&quot; alt=&quot;20180404111248861.jpg&quot; /&gt;&lt;/p&gt;

            </description>
            <pubDate>Wed, 04 Apr 2018 00:00:00 +0000</pubDate>
            <guid>
                https://knightyun.github.io/2018/04/04/negative-binary
            </guid>
        </item>
        
        <item>
            <title>搭建Github Pages个人博客网站</title>
            <link>
                https://knightyun.github.io/2018/04/01/github-pages-blog
            </link>
            <description>
                &lt;h3 id=&quot;目录-&quot;&gt;目录 &lt;span id=&quot;home&quot;&gt;&lt;/span&gt;&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#1&quot;&gt;引言&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#1.1&quot;&gt;关于博客&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#1.2&quot;&gt;关于Github&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#2&quot;&gt;创建Github账号&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#3&quot;&gt;创建仓库&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#3.1&quot;&gt;填充仓库&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#3.2&quot;&gt;配置Github Pages功能&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#4&quot;&gt;博客的书写与上传&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#4.1&quot;&gt;Git基础&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#4.2&quot;&gt;git配置&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#4.3&quot;&gt;git Desktop版&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#5&quot;&gt;创建本地仓库&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#6&quot;&gt;安装Jekyll&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#6.1&quot;&gt;关于Jekyll&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#6.2&quot;&gt;安装步骤&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#6.3&quot;&gt;开启jekyll&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#7&quot;&gt;写博客与上传&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#7.1&quot;&gt;Markdown基础&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#7.2&quot;&gt;工具介绍&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#7.3&quot;&gt;图床介绍&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#7.4&quot;&gt;关于图片尺寸&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#8&quot;&gt;域名配置&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;引言-&quot;&gt;引言 &lt;span id=&quot;1&quot;&gt;&lt;/span&gt;&lt;/h3&gt;

&lt;h4 id=&quot;关于博客-&quot;&gt;关于博客 &lt;span id=&quot;1.1&quot;&gt;&lt;/span&gt;&lt;/h4&gt;
&lt;p&gt;写博客对于程序猿来说，应该是个优秀的习惯，个人也觉得蛮高大上的 ^_^。网上的博客论坛网站也多种多样，个人觉得在长久以来的不断竞争淘汰中，各大网站的功能等可能都相差无几了，选择自己稍微偏好的就可以了。&lt;/p&gt;

&lt;p&gt;我的个人情况就是结合CSDN博客和Github Pages的独立个人博客网页，因为听说拥有自己的Github主页也是一件蛮高大上的事 -_- 。&lt;/p&gt;

&lt;h4 id=&quot;关于github-&quot;&gt;关于Github &lt;span id=&quot;1.2&quot;&gt;&lt;/span&gt;&lt;/h4&gt;

&lt;p&gt;然后简单介绍一下Github以及其Github Pages功能。&lt;/p&gt;

&lt;p&gt;GitHub是一个面向开源及私有软件项目的托管平台，也是一个分布式版本控制系统，详情见&lt;a href=&quot;(https://baike.baidu.com/item/github/10145341)&quot;&gt;百度百科&lt;/a&gt;。说到分布式，自然也有另外一种集中式版本控制系统：SVN，有兴趣小伙伴可以了解&lt;a href=&quot;https://baike.baidu.com/item/SVN/3311103?fr=aladdin&quot;&gt;百度百科&lt;/a&gt;。GIt是SVN的发展版，而且现在主流也是GIt，但某些大公司依然在使用SVN，二者各有优劣，自行体会，此处不做详解，用一张图简单说明：&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad359a34859f.png&quot; alt=&quot;20180331184953574.png&quot; /&gt;&lt;/p&gt;

&lt;hr /&gt;
&lt;p&gt;GIthub Pages则是github上的一项功能，可以放置网页文件到指定文件夹，然后给你一个专属域名用于展示一些项目，但现在大多用来开发制作个人博客网站。接下来就一步步按照我曾经的步骤来搭建个人博客，顺便讲讲沿途遇到过的坑，如没有的提及请自行百度。&lt;/p&gt;

&lt;h3 id=&quot;创建github账号-&quot;&gt;创建Github账号 &lt;span id=&quot;2&quot;&gt;&lt;/span&gt;&lt;/h3&gt;

&lt;p&gt;github pages 功能依赖于github账号，没有的话先去&lt;a href=&quot;https://github.com/&quot;&gt;官网&lt;/a&gt;注册一个：
&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad359e00a5bb.jpg&quot; alt=&quot;20180331192629516.jpg&quot; /&gt;
然后好像要邮箱验证，就是填写的那个，点击那个验证链接就注册成功了。&lt;/p&gt;

&lt;h3 id=&quot;创建仓库-&quot;&gt;创建仓库 &lt;span id=&quot;3&quot;&gt;&lt;/span&gt;&lt;/h3&gt;

&lt;p&gt;有了自己的账号后，可以跟着官网的引导，创建自己的第一个仓库，就是 &lt;strong&gt;repository&lt;/strong&gt;：
&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad359e5dd951.jpg&quot; alt=&quot;20180331193717261.jpg&quot; /&gt;&lt;/p&gt;

&lt;hr /&gt;
&lt;p&gt;填好信息
&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad359fb3ba18.jpg&quot; alt=&quot;20180331195826465.jpg&quot; /&gt;&lt;/p&gt;

&lt;hr /&gt;
&lt;p&gt;创建完成
&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad35a03b9186.jpg&quot; alt=&quot;20180331200133497.jpg&quot; /&gt;&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;到这里就创建好了自己的仓库，可以上传文件到这个目录下，接下我们用这个仓库来使用github pages功能。&lt;/p&gt;

&lt;h4 id=&quot;填充仓库-&quot;&gt;填充仓库 &lt;span id=&quot;3.1&quot;&gt;&lt;/span&gt;&lt;/h4&gt;

&lt;p&gt;仓库建好了，接下来就是往里面装东西了，就是支撑博客首页的一些网页文件和配置文件，对于新手来说要自己编写这些文件就有点开玩笑了，所以可以选择使用已有的主题，你可以选择复制我的&lt;a href=&quot;https://github.com/knightyun/knightyun.github.io&quot;&gt;https://github.com/knightyun/knightyun.github.io&lt;/a&gt;，然后选择自己仓库，网页基础好的同学以后修改网页内容就行了。&lt;/p&gt;
&lt;blockquote&gt;
  &lt;h5 id=&quot;嫌修改麻烦可以跳过这一步到后面的步骤选择喜欢的主题&quot;&gt;嫌修改麻烦可以跳过这一步，到后面的步骤选择喜欢的主题&lt;/h5&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad35a0cd1ddf.jpg&quot; alt=&quot;20180331204755801.jpg&quot; /&gt;&lt;/p&gt;

&lt;hr /&gt;

&lt;h4 id=&quot;配置github-pages功能-&quot;&gt;配置Github Pages功能 &lt;span id=&quot;3.2&quot;&gt;&lt;/span&gt;&lt;/h4&gt;

&lt;p&gt;然后我们来配置github pages&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad35a1317bee.jpg&quot; alt=&quot;20180331200909318.jpg&quot; /&gt;&lt;/p&gt;

&lt;hr /&gt;
&lt;p&gt;重命名，注意格式
&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad35a1ac689a.jpg&quot; alt=&quot;20180331202913817.jpg&quot; /&gt;&lt;/p&gt;

&lt;hr /&gt;
&lt;p&gt;把上面的页面向下滑，现在就可以访问了
&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad35ad3a3f49.jpg&quot; alt=&quot;20180331203510375.jpg&quot; /&gt;&lt;/p&gt;

&lt;hr /&gt;
&lt;p&gt;当然github也提供了一些主题供选择，点击上面的“choose a theme”按钮进行选择
&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad35ad9ad0e8.jpg&quot; alt=&quot;20180331205610766.jpg&quot; /&gt;&lt;/p&gt;

&lt;hr /&gt;
&lt;p&gt;这个网站有更多主题工选择：&lt;a href=&quot;http://jekyllthemes.org/&quot;&gt;http://jekyllthemes.org/&lt;/a&gt;，如有选择困难症请绕路 -_-&lt;/p&gt;

&lt;h3 id=&quot;博客的书写与上传-&quot;&gt;博客的书写与上传 &lt;span id=&quot;4&quot;&gt;&lt;/span&gt;&lt;/h3&gt;

&lt;h4 id=&quot;git基础-&quot;&gt;Git基础 &lt;span id=&quot;4.1&quot;&gt;&lt;/span&gt;&lt;/h4&gt;
&lt;p&gt;前面说到向自己的github仓库上传文件，我们使用“git”这个工具，进行拉取、克隆、提交等一系列操作，Linux系统应该是自带，官网下载地址：&lt;a href=&quot;https://git-scm.com/&quot;&gt;https://git-scm.com/&lt;/a&gt;。
并且需要掌握一些git基本操作，如 &lt;code class=&quot;highlighter-rouge&quot;&gt;git commit&lt;/code&gt; , &lt;code class=&quot;highlighter-rouge&quot;&gt;git push&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;git clone&lt;/code&gt; 等，这里有很完整的教程：&lt;a href=&quot;https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000&quot;&gt;Git语法说明&lt;/a&gt;.&lt;/p&gt;

&lt;h4 id=&quot;git配置-&quot;&gt;git配置 &lt;span id=&quot;4.2&quot;&gt;&lt;/span&gt;&lt;/h4&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;安装好后cmd输入 &lt;code class=&quot;highlighter-rouge&quot;&gt;git&lt;/code&gt; 有反应则安装成功：
&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad35adf42ae1.jpg&quot; alt=&quot;20180331215746907.jpg&quot; /&gt;&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;进行如下配置：&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;git config --global user.name &quot;YOUR NAME&quot;
git config --global user.email &quot;YOUR EMAIL ADDRESS&quot;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;blockquote&gt;
  &lt;h6 id=&quot;name指你的昵称email-address是你的注册邮箱&quot;&gt;NAME指你的昵称，EMAIL ADDRESS是你的注册邮箱&lt;/h6&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
  &lt;li&gt;然后生成相应的令牌，本地一份，Github 一份，这样 Github 可以在你使用仓库的时候，进行校验确定你的身份。&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;cd ~/.ssh
mkdir key_backup
ssh-keygen -t rsa -C &quot;*your_email@youremail.com*&quot;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;blockquote&gt;
  &lt;h6 id=&quot;注意这里不是在cmd里输入是使用刚安装的-git-bash-软件可以在电脑菜单里面搜索&quot;&gt;注意这里不是在cmd里输入，是使用刚安装的 git bash 软件，可以在电脑菜单里面搜索&lt;/h6&gt;
&lt;/blockquote&gt;

&lt;p&gt;然后会生成如下两个文件：
&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad35ae510df3.jpg&quot; alt=&quot;20180331221012285.jpg&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;id_rsa.pub&lt;/code&gt; 就是我们待会需要的公钥文件，使用命令 &lt;code class=&quot;highlighter-rouge&quot;&gt;cat id_rsa.pub&lt;/code&gt; 再将内容复制到剪切板，然后进入github账号设置里面粘贴&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad35aea5e8c2.jpg&quot; alt=&quot;20180331221601652.jpg&quot; /&gt;&lt;/p&gt;

&lt;hr /&gt;
&lt;p&gt;选择添加SSH key：
&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad35afc4616a.jpg&quot; alt=&quot;20180331221949179.jpg&quot; /&gt;&lt;/p&gt;

&lt;hr /&gt;
&lt;p&gt;把刚才复制的内容粘贴进去
&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad35afc52683.jpg&quot; alt=&quot;20180331222325449.jpg&quot; /&gt;&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;然后输入 &lt;code class=&quot;highlighter-rouge&quot;&gt;ssh -T git@github.com&lt;/code&gt; 测试连通状态&lt;/p&gt;
&lt;blockquote&gt;
  &lt;h6 id=&quot;我的windows版没有成功不知道linux是否成功报错如下-应该是windows-ssh配置问题&quot;&gt;我的Windows版没有成功，不知道Linux是否成功，报错如下 ，应该是windows ssh配置问题&lt;/h6&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad35b07f16b6.jpg&quot; alt=&quot;20180331222821644.jpg&quot; /&gt;&lt;/p&gt;

&lt;h4 id=&quot;git-desktop版-&quot;&gt;git Desktop版 &lt;span id=&quot;4.3&quot;&gt;&lt;/span&gt;&lt;/h4&gt;

&lt;p&gt;如果你也出现以上状况，不必担心，git还能使用https协议连接，只不过要每次输入账号和密码，但是可以选择github官方提供的git desktop软件：&lt;/p&gt;
&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;这里我是下载过的&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad35b2fb62f6.jpg&quot; alt=&quot;20180331223500277.jpg&quot; /&gt;&lt;/p&gt;

&lt;hr /&gt;
&lt;p&gt;界面如下，需要登录，以后提交文件就方便了，cmd也能使用git提交，不用每次输入密码
&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad35b347f466.jpg&quot; alt=&quot;20180331223916609.jpg&quot; /&gt;&lt;/p&gt;

&lt;hr /&gt;
&lt;p&gt;可以查看变化文件，甚至文件内变化的内容，commit 后点击 fetch 按钮提交
&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad35c4838fe1.jpg&quot; alt=&quot;20180401085504366.jpg&quot; /&gt;&lt;/p&gt;
&lt;blockquote&gt;
  &lt;h6 id=&quot;软件功能不算复杂自己摸索一会就会了图形界面的软件使得一些命令行的操作变得容易友好&quot;&gt;软件功能不算复杂，自己摸索一会就会了，图形界面的软件使得一些命令行的操作变得容易、友好。&lt;/h6&gt;
&lt;/blockquote&gt;

&lt;h3 id=&quot;创建本地仓库-&quot;&gt;创建本地仓库 &lt;span id=&quot;5&quot;&gt;&lt;/span&gt;&lt;/h3&gt;
&lt;p&gt;选择一个本地文件夹，用作保存本地仓库文件，尽量是空文件夹，然后使用命令 &lt;code class=&quot;highlighter-rouge&quot;&gt;git init&lt;/code&gt; 初始化文件夹，其实是在当前文件夹下生成一个叫 &lt;code class=&quot;highlighter-rouge&quot;&gt;.git&lt;/code&gt; 的隐藏文件夹，里面是一些配置文件，不要随意更改。&lt;/p&gt;

&lt;p&gt;使用 &lt;code class=&quot;highlighter-rouge&quot;&gt;git clone https://github.com/name/repository.git&lt;/code&gt; 将远程仓库克隆到本地此文件夹下，
&lt;code class=&quot;highlighter-rouge&quot;&gt;name&lt;/code&gt; 是自己的昵称，&lt;code class=&quot;highlighter-rouge&quot;&gt;repository&lt;/code&gt; 是自己的仓库名，不要忘记末尾的 &lt;strong&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;.git&lt;/code&gt;&lt;/strong&gt; 后缀。&lt;/p&gt;

&lt;p&gt;然后此文件夹下会多一个和你仓储名一样的文件夹，内部文件与远程仓库一样。
&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad35c4e8b088.jpg&quot; alt=&quot;20180331230145814.jpg&quot; /&gt;
绑定远程仓库，方便提交：&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;git remote add origin git@github.com:username/username.github.io.git
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;介绍几个常用命令：&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;git add . //添加文件
git commit -m &quot;commit-messages&quot; //提交本地仓库
git push origin master //提交远程仓库
git pull //拉取远程文件，与以下命令类似
git branch temp //创建本地分支
git fetch origin master:temp
git merge master
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;安装jekyll-&quot;&gt;安装Jekyll &lt;span id=&quot;6&quot;&gt;&lt;/span&gt;&lt;/h3&gt;

&lt;h4 id=&quot;关于jekyll-&quot;&gt;关于Jekyll &lt;span id=&quot;6.1&quot;&gt;&lt;/span&gt;&lt;/h4&gt;

&lt;p&gt;Jekyll是一个简单免费的生成博客网页的工具，可以绑定github，详情参考官网：&lt;a href=&quot;https://jekyllrb.com/&quot;&gt;https://jekyllrb.com/&lt;/a&gt;, 也有一个中文版的：&lt;a href=&quot;https://www.jekyll.com.cn/&quot;&gt;https://www.jekyll.com.cn/&lt;/a&gt; 方便阅读。我的博客就是通过jekyll建立了，上面那个主题网站也是jekyll的，还有一个类似的工具叫“hexo”，自行了解。&lt;/p&gt;

&lt;blockquote&gt;
  &lt;h6 id=&quot;上传修改后的文件到github仓库后需要一段时间才能看到网页的变化或修改效果所以可以选择安装本地jekyll工具进行本地快速预览&quot;&gt;上传修改后的文件到github仓库后需要一段时间才能看到网页的变化或修改效果，所以可以选择安装本地jekyll工具进行本地快速预览。&lt;/h6&gt;
&lt;/blockquote&gt;

&lt;h4 id=&quot;安装步骤-&quot;&gt;安装步骤 &lt;span id=&quot;6.2&quot;&gt;&lt;/span&gt;&lt;/h4&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;安装Ruby&lt;/strong&gt;：jekyll依赖于Ruby，需要提前安装，官网下载链接：&lt;a href=&quot;http://www.ruby-lang.org/en/downloads/&quot;&gt;http://www.ruby-lang.org/en/downloads/&lt;/a&gt;，windows/Linux/Mac的版本都有。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;安装gem&lt;/strong&gt;：官网链接&lt;a href=&quot;https://rubygems.org/pages/download&quot;&gt;https://rubygems.org/pages/download&lt;/a&gt;，貌似安装ruby后自带gem。
cmd命令行输入 &lt;code class=&quot;highlighter-rouge&quot;&gt;gem&lt;/code&gt; 检查是否安装成功：
&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad35c5295bb6.jpg&quot; alt=&quot;20180331213445607.jpg&quot; /&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;安装jekyll&lt;/strong&gt;：cmd命令行输入 &lt;code class=&quot;highlighter-rouge&quot;&gt;gem install jekyll&lt;/code&gt;
&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad35c57c50b5.jpg&quot; alt=&quot;20180331213739704.jpg&quot; /&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;开启jekyll-&quot;&gt;开启jekyll &lt;span id=&quot;6.3&quot;&gt;&lt;/span&gt;&lt;/h4&gt;

&lt;p&gt;直接输入 &lt;code class=&quot;highlighter-rouge&quot;&gt;jekyll s&lt;/code&gt; 开启jekyll服务，windows可能会遇到以下问题：
&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad35c5c2c9f1.jpg&quot; alt=&quot;20180401085818819.jpg&quot; /&gt;&lt;/p&gt;
&lt;blockquote&gt;
  &lt;h6 id=&quot;-使用-bundle-exec-jekyll-s-命令就可以运行了如果提示没有安装-bundler-就-gem-install-bundler-再-bundle-install&quot;&gt;* 使用 &lt;code class=&quot;highlighter-rouge&quot;&gt;bundle exec jekyll s&lt;/code&gt; 命令就可以运行了，如果提示没有安装 &lt;code class=&quot;highlighter-rouge&quot;&gt;bundler&lt;/code&gt; ，就 &lt;code class=&quot;highlighter-rouge&quot;&gt;gem install bundler&lt;/code&gt; 再 &lt;code class=&quot;highlighter-rouge&quot;&gt;bundle install&lt;/code&gt;&lt;/h6&gt;
  &lt;h6 id=&quot;-可能还会提示没有安装其他组件记下名称-gem-install-xxx-就可以了&quot;&gt;* 可能还会提示没有安装其他组件，记下名称， &lt;code class=&quot;highlighter-rouge&quot;&gt;gem install xxx&lt;/code&gt; 就可以了&lt;/h6&gt;
&lt;/blockquote&gt;

&lt;p&gt;然后就可以成功运行了，退出按 &lt;code class=&quot;highlighter-rouge&quot;&gt;ctrl + c &lt;/code&gt; 键
&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad35c5fa50e4.jpg&quot; alt=&quot;20180401085938699.jpg&quot; /&gt;&lt;/p&gt;
&lt;blockquote&gt;
  &lt;h6 id=&quot;-运行时保持这个窗口不要关闭浏览器输入-1270014000-或-localhost4000-进行预览&quot;&gt;* 运行时保持这个窗口不要关闭，浏览器输入 &lt;code class=&quot;highlighter-rouge&quot;&gt;127.0.0.1:4000&lt;/code&gt; 或 &lt;code class=&quot;highlighter-rouge&quot;&gt;localhost:4000&lt;/code&gt; 进行预览&lt;/h6&gt;
  &lt;h6 id=&quot;-不过我的windows预览效果不太好加载不出图片其他系统没试过&quot;&gt;* 不过我的windows预览效果不太好，加载不出图片，其他系统没试过&lt;/h6&gt;
&lt;/blockquote&gt;

&lt;h3 id=&quot;写博客与上传-&quot;&gt;写博客与上传 &lt;span id=&quot;7&quot;&gt;&lt;/span&gt;&lt;/h3&gt;

&lt;h4 id=&quot;markdown基础-&quot;&gt;Markdown基础 &lt;span id=&quot;7.1&quot;&gt;&lt;/span&gt;&lt;/h4&gt;
&lt;p&gt;Jekyll使用&lt;a href=&quot;https://baike.baidu.com/item/markdown/3245829?fr=aladdin&quot;&gt;Markdown&lt;/a&gt;语言书写博客，markdown是一种简单易读的标记性语言，不同于 &lt;code class=&quot;highlighter-rouge&quot;&gt;html&lt;/code&gt;，大量的标签不易于阅读，写着也麻烦，用markdown写博客很合适。&lt;/p&gt;

&lt;p&gt;首先你需要了解一些markdown语法，这里有完整版语法说明：&lt;a href=&quot;https://blog.csdn.net/KNIGH_YUN/article/details/79733814&quot;&gt;Markdown语法说明&lt;/a&gt;，了解一些基础后就可以开始写博客了。&lt;/p&gt;

&lt;h4 id=&quot;工具介绍-&quot;&gt;工具介绍 &lt;span id=&quot;7.2&quot;&gt;&lt;/span&gt;&lt;/h4&gt;
&lt;p&gt;这篇文章：&lt;a href=&quot;https://blog.csdn.net/KNIGH_YUN/article/details/79718481&quot;&gt;Markdown简明语法&lt;/a&gt;最后有介绍一些好用的markdown编辑器，自行选择。
不过每次都用编辑器写好 &lt;code class=&quot;highlighter-rouge&quot;&gt;.md&lt;/code&gt; 文件然后用 git 上传到 github 根目录下的 &lt;strong&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;_post&lt;/code&gt;&lt;/strong&gt; 文件夹好像很繁琐，Jekyll官方提供了一款方便的博客编辑器，方便书写、预览、上传，官网链接：&lt;a href=&quot;http://jekyllwriter.com/&quot;&gt;http://jekyllwriter.com/&lt;/a&gt;，三种系统版本都有。接下来简单介绍一些使用：&lt;/p&gt;

&lt;p&gt;安装后主界面：
&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad35c729800f.jpg&quot; alt=&quot;20180401093410604.jpg&quot; /&gt;&lt;/p&gt;

&lt;hr /&gt;
&lt;p&gt;添加账号
&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad35c786326c.jpg&quot; alt=&quot;20180401093825352.jpg&quot; /&gt;&lt;/p&gt;

&lt;p&gt;配置 token
&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad35c7dc4e18.jpg&quot; alt=&quot;20180401094705731.jpg&quot; /&gt;
保存后会生成一个 token ，返回软件粘贴进输入框就行了
&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad35c836423d.jpg&quot; alt=&quot;20180401094911796.jpg&quot; /&gt;&lt;/p&gt;

&lt;hr /&gt;
&lt;p&gt;写完后保存并上传
&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad35c88b1b6f.jpg&quot; alt=&quot;20180401094200638.jpg&quot; /&gt;&lt;/p&gt;

&lt;hr /&gt;
&lt;p&gt;可以在这里查看和修改账户下的博客
&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad35c8e19921.jpg&quot; alt=&quot;2018040109525674.jpg&quot; /&gt;&lt;/p&gt;

&lt;hr /&gt;
&lt;p&gt;&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad35c92b3b2b.jpg&quot; alt=&quot;20180401095443179.jpg&quot; /&gt;&lt;/p&gt;

&lt;blockquote&gt;
  &lt;h6 id=&quot;软件其他功能还在完善自行摸索&quot;&gt;软件其他功能还在完善，自行摸索&lt;/h6&gt;
&lt;/blockquote&gt;

&lt;h4 id=&quot;图床介绍-&quot;&gt;图床介绍 &lt;span id=&quot;7.3&quot;&gt;&lt;/span&gt;&lt;/h4&gt;
&lt;p&gt;写博客就无法避免上传图片，图床就是这么一个地方，就是一个网站，你发自己的图片上传到它的网站，然后它给你一个这个图片的链接，插入博客中就能显示图片了。&lt;/p&gt;

&lt;p&gt;推荐一个知名的，七牛云&lt;a href=&quot;https://portal.qiniu.com/&quot;&gt;https://portal.qiniu.com/&lt;/a&gt;，注册完实名认证后有一些优惠。
还有一个神奇的网站：&lt;a href=&quot;https://sm.ms/&quot;&gt;https://sm.ms/&lt;/a&gt;，也能用&lt;/p&gt;

&lt;p&gt;然后在 jekyll writer中配置一下：
&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad35c9795b52.jpg&quot; alt=&quot;20180401100517435.jpg&quot; /&gt;&lt;/p&gt;

&lt;hr /&gt;
&lt;blockquote&gt;
  &lt;h6 id=&quot;当然我用的是csdn在线编辑器写博客图片能直接上传到csdn上直接生成链接其工具也能用&quot;&gt;当然我用的是CSDN在线编辑器写博客，图片能直接上传到CSDN上，直接生成链接，其工具也能用&lt;/h6&gt;
&lt;/blockquote&gt;

&lt;h4 id=&quot;关于图片尺寸-&quot;&gt;关于图片尺寸 &lt;span id=&quot;7.4&quot;&gt;&lt;/span&gt;&lt;/h4&gt;
&lt;p&gt;markdown的图片插入方式 &lt;code class=&quot;highlighter-rouge&quot;&gt;![title](http://xxx.com/xxx.png/)&lt;/code&gt; 是没办法修改图片尺寸的，可以使用html中的 &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;img&amp;gt;&lt;/code&gt;标签：
&lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;img src=&quot;http://xxx.com/xxx.png/&quot; alt=&quot;title&quot; width=XXpx height=XXpx&amp;gt;&lt;/code&gt;
&lt;code class=&quot;highlighter-rouge&quot;&gt;width&lt;/code&gt; 和 &lt;code class=&quot;highlighter-rouge&quot;&gt;height&lt;/code&gt; 添加想要的尺寸。&lt;/p&gt;

&lt;h3 id=&quot;域名配置-&quot;&gt;域名配置 &lt;span id=&quot;8&quot;&gt;&lt;/span&gt;&lt;/h3&gt;

&lt;p&gt;自己的博客网站就建好了，想要分享出去的小伙伴就要想办法让自己的网页能被百度等搜索引擎搜到，或者这样，百度搜索： &lt;code class=&quot;highlighter-rouge&quot;&gt;site:name.github.io&lt;/code&gt; ，出现错误页面就表示搜不到。&lt;/p&gt;

&lt;p&gt;很遗憾，百度是禁止抓取 github pages 的内容的，可以购买一个自己的专属域名，有很多选择，阿里云、腾讯、花生壳域名等，百度站长平台有个链接提交功能，但是它只是加速爬取，并未解决收录：
&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad35ca96986d.jpg&quot; alt=&quot;20180401103839442.jpg&quot; /&gt;&lt;/p&gt;

&lt;hr /&gt;
&lt;p&gt;貌似它们的熊掌号服务可以解决这问题：
&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad35cb06c1c5.jpg&quot; alt=&quot;20180401104100873.jpg&quot; /&gt;&lt;/p&gt;

&lt;hr /&gt;
&lt;p&gt;然后，就没有然后了 -_-
&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad35cb55c7cd.jpg&quot; alt=&quot;20180401104147731.jpg&quot; /&gt;&lt;/p&gt;

&lt;hr /&gt;
&lt;p&gt;以花生壳域名为例，其它大同小异，配置一下：
&lt;img src=&quot;https://i.loli.net/2018/04/15/5ad35cb937be5.jpg&quot; alt=&quot;20180401102926509.jpg&quot; /&gt;&lt;/p&gt;

&lt;hr /&gt;
&lt;p&gt;再添加两条 github 的ip的 A记录值 ：&lt;code class=&quot;highlighter-rouge&quot;&gt;192.30.252.153&lt;/code&gt; &lt;code class=&quot;highlighter-rouge&quot;&gt;192.30.252.154&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;最后搜索: &lt;code class=&quot;highlighter-rouge&quot;&gt;&quot;site:你的域名&quot;&lt;/code&gt; 有结果就成功了&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;开始自己的博客生涯吧。&lt;/p&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;返回顶部&quot;&gt;返回&lt;a href=&quot;#home&quot;&gt;顶部&lt;/a&gt;&lt;/h3&gt;

            </description>
            <pubDate>Sun, 01 Apr 2018 00:00:00 +0000</pubDate>
            <guid>
                https://knightyun.github.io/2018/04/01/github-pages-blog
            </guid>
        </item>
        
        <item>
            <title>C语言统计素数</title>
            <link>
                https://knightyun.github.io/2018/03/30/c-prime-number
            </link>
            <description>
                &lt;h3 id=&quot;源码如下&quot;&gt;源码如下：&lt;/h3&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;#include&amp;lt;stdio.h&amp;gt;
int main()
{
    // 1-1000的素数
    printf(&quot;1-1000的素数: \n\n&quot;); 
    int i, j, co = 0; //设置co用于统计个数
    for (j = 1; j &amp;lt;= 1000; j++) //遍历1-1000
    {
        for (i = 2; i &amp;lt; j; i++) //素数条件：只能被1和自身整除
        {
            if (j % i == 0) //发现不是素数，则跳出本层循环
            {
                break;
            }
        }
        if (i == j) //遍历后，没有除1和自身以外的因数，i==j 表示遍历了 2--自身的所有数并且未发现因数
	{
            printf(&quot;%d &quot;, j);
            co++;
	}
    }
    printf(&quot;\nTotal: %d\n\n&quot;, co);
}	   
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;运行结果&quot;&gt;运行结果：&lt;/h3&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;1-1000的素数:

2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 
103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 
199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 
313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 
433 439 443 449 457 461 463 467 479 487 491 499 503 509 521 523 541 547 557 
563 569 571 577 587 593 599 601 607 613 617 619 631 641 643 647 653 659 661 
673 677 683 691 701 709 719 727 733 739 743 751 757 761 769 773 787 797 809 
811 821 823 827 829 839 853 857 859 863 877 881 883 887 907 911 919 929 937 
941 947 953 967 971 977 983 991 997

total: 168
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;注意&quot;&gt;注意&lt;/h3&gt;

&lt;p&gt;素数，又称质数，即整数在一个大于1的自然数中，除了1和此整数自身外，没法被其他自然数整除的数。将1-1000中每个数都对“2–自身”进行整除，&lt;code class=&quot;highlighter-rouge&quot;&gt;i==j&lt;/code&gt; 说明是对“2–自身”都遍历了一遍并且未发现符合条件的数，而不是前面发现有多余因数的&lt;code class=&quot;highlighter-rouge&quot;&gt;break;&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
  &lt;h4 id=&quot;另一个优化的版本供参考&quot;&gt;另一个优化的版本，供参考：&lt;/h4&gt;
&lt;/blockquote&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;#include&amp;lt;stdio.h&amp;gt;
#include&amp;lt;math.h&amp;gt;
int main()
{
    // 1-1000的素数
    printf(&quot;1-1000的素数: \n\n&quot;); 
    int i, j, co = 0;
    printf(&quot;2 &quot;);
    for (j = 1; j &amp;lt;= 1000; j+=2) //不遍历偶数，除了2偶数都不是质数
    {
	for (i = 2; i &amp;lt;= sqrt(j); i++) //实际遍历到此数的平方根就够了，需要引入 &amp;lt;math.h&amp;gt; 头文件
	{
            if (j % i == 0)
	    {
		break;
	    }
	}
	if (i &amp;gt; sqrt(j)) //这里也要变一下
	{
	    printf(&quot;%d &quot;, j);
	    co++;
	}
    }
    printf(&quot;\n\ntotal: %d\n\n&quot;, co+1); //注意这里要加上“2”这个数，2是质数也是偶数，之前没有遍历
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;这样就大大缩短了时间。&lt;/p&gt;

            </description>
            <pubDate>Fri, 30 Mar 2018 00:00:00 +0000</pubDate>
            <guid>
                https://knightyun.github.io/2018/03/30/c-prime-number
            </guid>
        </item>
        
        <item>
            <title>C语言打印九九乘法表</title>
            <link>
                https://knightyun.github.io/2018/03/30/c-multiply-table
            </link>
            <description>
                &lt;h2 id=&quot;源码如下&quot;&gt;源码如下：&lt;/h2&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;#include&amp;lt;stdio.h&amp;gt;
int main()
{
	//打印九九乘法表
	printf(&quot;九九乘法表：\n&quot;); 
	int x, y; //初始化打印的两个方向
	for (y = 1; y &amp;lt; 10; y++) //两层循环嵌套打印输出
	{
		for (x = 1; x &amp;lt;= y; x++)
		{
			printf(&quot;%d*%d=%2d &quot;, y, x, x * y); //%2d表示固定输出两位
		}
		printf(&quot;\n&quot;); //打印到行尾，换行
	}
	printf(&quot;This is the end.\n&quot;);
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;输出结果&quot;&gt;输出结果：&lt;/h2&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;九九乘法表：
1*1= 1
2*1= 2 2*2= 4
3*1= 3 3*2= 6 3*3= 9
4*1= 4 4*2= 8 4*3=12 4*4=16
5*1= 5 5*2=10 5*3=15 5*4=20 5*5=25
6*1= 6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36
7*1= 7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49
8*1= 8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64
9*1= 9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81
This is the end.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;注意&quot;&gt;注意：&lt;/h2&gt;

&lt;p&gt;代码注释处说明的&lt;code class=&quot;highlighter-rouge&quot;&gt;%2d&lt;/code&gt;是为了美观，强行使相乘的结果占两个字符位，否则打印结果如下：&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;九九乘法表：
1*1=1
2*1=2 2*2=4
3*1=3 3*2=6 3*3=9
4*1=4 4*2=8 4*3=12 4*4=16
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36
7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64
9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81
This is the end.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;没有上面美观吧-_-&lt;/p&gt;

            </description>
            <pubDate>Fri, 30 Mar 2018 00:00:00 +0000</pubDate>
            <guid>
                https://knightyun.github.io/2018/03/30/c-multiply-table
            </guid>
        </item>
        
        <item>
            <title>C语言统计闰年</title>
            <link>
                https://knightyun.github.io/2018/03/30/c-leap-year
            </link>
            <description>
                &lt;h3 id=&quot;源码如下&quot;&gt;源码如下：&lt;/h3&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;#include&amp;lt;stdio.h&amp;gt;
int main()
{
    // 统计1-2020的闰年
    printf(&quot;1-2020的闰年: \n\n&quot;); 
    int ye, c = 0;
    for (ye = 1; ye &amp;lt;= 2020; ye++) //此处也可更改为指定年份区间
    {
        if (((ye % 4 == 0) &amp;amp;&amp;amp; (ye % 100 != 0)) || (ye % 400 == 0)) //闰年的定义：能被4整除但不能被100整除，或能被400整除的年份
        {
            printf(&quot;%d &quot;, ye);
            c++;
        }
    }
    printf(&quot;\n\nTotal: %d\n&quot;, c); //用定义的变量c统计
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;运行结果&quot;&gt;运行结果：&lt;/h3&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;1-2020的闰年:

4 8 12 16 20 24 28 32 36 40 44 48 52 56 60 64 68 72 76 80 84 88 92 96 104 
108 112 116 120 124 128 132 136 140 144 148 152 156 160 164 168 172 176 
180 184 188 192 196 204 208 212 216 220 224 228 232 236 240 244 248 252 
256 260 264 268 272 276 280 284 288 292 296 304 308 312 316 320 324 328 
332 336 340 344 348 352 356 360 364 368 372 376 380 384 388 392 396 400 
404 408 412 416 420 424 428 432 436 440 444 448 452 456 460 464 468 472 
476 480 484 488 492 496 504 508 512 516 520 524 528 532 536 540 544 548 
552 556 560 564 568 572 576 580 584 588 592 596 604 608 612 616 620 624 
628 632 636 640 644 648 652 656 660 664 668 672 676 680 684 688 692 696 
704 708 712 716 720 724 728 732 736 740 744 748 752 756 760 764 768 772 
776 780 784 788 792 796 800 804 808 812 816 820 824 828 832 836 840 844 
848 852 856 860 864 868 872 876 880 884 888 892 896 904 908 912 916 920 
924 928 932 936 940 944 948 952 956 960 964 968 972 976 980 984 988 992 
996 1004 1008 1012 1016 1020 1024 1028 1032 1036 1040 1044 1048 1052 
1056 1060 1064 1068 1072 1076 1080 1084 1088 1092 1096 1104 1108 1112 
1116 1120 1124 1128 1132 1136 1140 1144 1148 1152 1156 1160 1164 1168 
1172 1176 1180 1184 1188 1192 1196 1200 1204 1208 1212 1216 1220 1224 
1228 1232 1236 1240 1244 1248 1252 1256 1260 1264 1268 1272 1276 1280 
1284 1288 1292 1296 1304 1308 1312 1316 1320 1324 1328 1332 1336 1340 
1344 1348 1352 1356 1360 1364 1368 1372 1376 1380 1384 1388 1392 1396 
1404 1408 1412 1416 1420 1424 1428 1432 1436 1440 1444 1448 1452 1456 
1460 1464 1468 1472 1476 1480 1484 1488 1492 1496 1504 1508 1512 1516 
1520 1524 1528 1532 1536 1540 1544 1548 1552 1556 1560 1564 1568 1572 
1576 1580 1584 1588 1592 1596 1600 1604 1608 1612 1616 1620 1624 1628 
1632 1636 1640 1644 1648 1652 1656 1660 1664 1668 1672 1676 1680 1684 
1688 1692 1696 1704 1708 1712 1716 1720 1724 1728 1732 1736 1740 1744 
1748 1752 1756 1760 1764 1768 1772 1776 1780 1784 1788 1792 1796 1804 
1808 1812 1816 1820 1824 1828 1832 1836 1840 1844 1848 1852 1856 1860 
1864 1868 1872 1876 1880 1884 1888 1892 1896 1904 1908 1912 1916 1920 
1924 1928 1932 1936 1940 1944 1948 1952 1956 1960 1964 1968 1972 1976 
1980 1984 1988 1992 1996 2000 2004 2008 2012 2016 2020

Total: 490
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

            </description>
            <pubDate>Fri, 30 Mar 2018 00:00:00 +0000</pubDate>
            <guid>
                https://knightyun.github.io/2018/03/30/c-leap-year
            </guid>
        </item>
        
        <item>
            <title>Markdown完整语法说明</title>
            <link>
                https://knightyun.github.io/2018/03/28/markdown-total
            </link>
            <description>
                &lt;h2 id=&quot;markdown-完整语法说明-&quot;&gt;Markdown 完整语法说明 &lt;span id=&quot;home&quot;&gt;&lt;/span&gt;&lt;/h2&gt;

&lt;hr /&gt;

&lt;p&gt;辛勤的搬运工，原文链接&lt;a href=&quot;http://wowubuntu.com/markdown/index.html&quot;&gt;http://wowubuntu.com/markdown/index.html&lt;/a&gt;&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;目录&quot;&gt;目录&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;&lt;a href=&quot;#1&quot;&gt;概述 &lt;/a&gt;&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;strong&gt;&lt;a href=&quot;#1.1&quot;&gt;宗旨 &lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;&lt;a href=&quot;#1.2&quot;&gt;兼容 HTML &lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;&lt;a href=&quot;#1.3&quot;&gt;特殊字符自动转换 &lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;&lt;a href=&quot;#2&quot;&gt;区块元素 &lt;/a&gt;&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;strong&gt;&lt;a href=&quot;#2.1&quot;&gt;段落和换行 &lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;&lt;a href=&quot;#2.2&quot;&gt;标题 &lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;&lt;a href=&quot;#2.3&quot;&gt;区块引用 Blockquotes &lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;&lt;a href=&quot;#2.4&quot;&gt;列表 &lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;&lt;a href=&quot;#2.5&quot;&gt;分隔线 &lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;&lt;a href=&quot;#3&quot;&gt;区段元素 &lt;/a&gt;&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;strong&gt;&lt;a href=&quot;#3.1&quot;&gt;链接 &lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;&lt;a href=&quot;#3.2&quot;&gt;强调 &lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;&lt;a href=&quot;#3.3&quot;&gt;代码 &lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;&lt;a href=&quot;#4&quot;&gt;其它 &lt;/a&gt;&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;strong&gt;&lt;a href=&quot;#4.1&quot;&gt;自动链接 &lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;&lt;a href=&quot;#4.2&quot;&gt;反斜杠 &lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;概述-&quot;&gt;概述 &lt;span id=&quot;1&quot;&gt;&lt;/span&gt;&lt;/h2&gt;

&lt;h3 id=&quot;宗旨-&quot;&gt;宗旨 &lt;span id=&quot;1.1&quot;&gt;&lt;/span&gt;&lt;/h3&gt;

&lt;p&gt;Markdown 的目标是实现「易读易写」。&lt;/p&gt;

&lt;p&gt;可读性，无论如何，都是最重要的。一份使用 Markdown 格式撰写的文件应该可以直接以纯文本发布，并且看起来不会像是由许多标签或是格式指令所构成。Markdown 语法受到一些既有 text-to-HTML 格式的影响，包括 Setext、atx、Textile、reStructuredText、Grutatext 和 EtText，而最大灵感来源其实是纯文本电子邮件的格式。&lt;/p&gt;

&lt;p&gt;总之， Markdown 的语法全由一些符号所组成，这些符号经过精挑细选，其作用一目了然。比如：在文字两旁加上星号，看起来就像&lt;em&gt;强调&lt;/em&gt;。Markdown 的列表看起来，嗯，就是列表。Markdown 的区块引用看起来就真的像是引用一段文字，就像你曾在电子邮件中见过的那样。&lt;/p&gt;

&lt;h3 id=&quot;兼容-html-&quot;&gt;兼容 HTML &lt;span id=&quot;1.2&quot;&gt;&lt;/span&gt;&lt;/h3&gt;

&lt;p&gt;Markdown 语法的目标是：成为一种适用于网络的书写语言。&lt;/p&gt;

&lt;p&gt;Markdown 不是想要取代 HTML，甚至也没有要和它相近，它的语法种类很少，只对应 HTML 标记的一小部分。Markdown 的构想不是要使得 HTML 文档更容易书写。在我看来， HTML 已经很容易写了。Markdown 的理念是，能让文档更容易读、写和随意改。HTML 是一种发布的格式，Markdown 是一种书写的格式。就这样，Markdown 的格式语法只涵盖纯文本可以涵盖的范围。&lt;/p&gt;

&lt;p&gt;不在 Markdown 涵盖范围之内的标签，都可以直接在文档里面用 HTML 撰写。不需要额外标注这是 HTML 或是 Markdown；只要直接加标签就可以了。&lt;/p&gt;

&lt;p&gt;要制约的只有一些 HTML 区块元素――比如 &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;div&amp;gt;&lt;/code&gt;、&lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;table&amp;gt;&lt;/code&gt;、&lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;pre&amp;gt;&lt;/code&gt;、&lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;p&amp;gt;&lt;/code&gt; 等标签，必须在前后加上空行与其它内容区隔开，还要求它们的开始标签与结尾标签不能用制表符或空格来缩进。Markdown 的生成器有足够智能，不会在 HTML 区块标签外加上不必要的 &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;p&amp;gt;&lt;/code&gt; 标签。&lt;/p&gt;

&lt;p&gt;例子如下，在 Markdown 文件里加上一段 HTML 表格：&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;这是一个普通段落。

&amp;lt;table&amp;gt;
    &amp;lt;tr&amp;gt;
        &amp;lt;td&amp;gt;Foo&amp;lt;/td&amp;gt;
    &amp;lt;/tr&amp;gt;
&amp;lt;/table&amp;gt;

这是另一个普通段落。 请注意，在 HTML 区块标签间的 Markdown 格式语法将不会被处理。比如，你在 HTML 区块内使用 Markdown 样式的`*强调*`会没有效果。
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;HTML 的区段（行内）标签如 &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;span&amp;gt;&lt;/code&gt;、&lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;cite&amp;gt;&lt;/code&gt;、&lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;del&amp;gt;&lt;/code&gt; 可以在 Markdown 的段落、列表或是标题里随意使用。依照个人习惯，甚至可以不用 Markdown 格式，而直接采用 HTML 标签来格式化。举例说明：如果比较喜欢 HTML 的 &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;a&amp;gt;&lt;/code&gt; 或 &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;img&amp;gt;&lt;/code&gt; 标签，可以直接使用这些标签，而不用 Markdown 提供的链接或是图像标签语法。&lt;/p&gt;

&lt;p&gt;和处在 HTML 区块标签间不同，Markdown 语法在 HTML 区段标签间是有效的。&lt;/p&gt;

&lt;h3 id=&quot;特殊字符自动转换-&quot;&gt;特殊字符自动转换 &lt;span id=&quot;1.3&quot;&gt;&lt;/span&gt;&lt;/h3&gt;

&lt;p&gt;在 HTML 文件中，有两个字符需要特殊处理： &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;&lt;/code&gt; 和 &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;amp;&lt;/code&gt; 。 &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;&lt;/code&gt; 符号用于起始标签，&lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;amp;&lt;/code&gt; 符号则用于标记 HTML 实体，如果你只是想要显示这些字符的原型，你必须要使用实体的形式，像是 &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;amp;lt;&lt;/code&gt; 和 &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;amp;amp;&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;amp;&lt;/code&gt; 字符尤其让网络文档编写者受折磨，如果你要打「&lt;code class=&quot;highlighter-rouge&quot;&gt;AT&amp;amp;T&lt;/code&gt;」 ，你必须要写成「&lt;code class=&quot;highlighter-rouge&quot;&gt;AT&amp;amp;amp;T&lt;/code&gt;」。而网址中的&lt;code class=&quot;highlighter-rouge&quot;&gt; &amp;amp;&lt;/code&gt; 字符也要转换。比如你要链接到：&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;http://images.google.com/images?num=30&amp;amp;q=larry+bird 你必须要把网址转换写为：

http://images.google.com/images?num=30&amp;amp;amp;q=larry+bird 才能放到链接标签的 `href` 属性里。不用说也知道这很容易忽略，这也可能是 HTML 标准检验所检查到的错误中，数量最多的。
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Markdown 让你可以自然地书写字符，需要转换的由它来处理好了。如果你使用的&lt;code class=&quot;highlighter-rouge&quot;&gt; &amp;amp; &lt;/code&gt;字符是 HTML 字符实体的一部分，它会保留原状，否则它会被转换成 &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;amp;amp;&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;所以你如果要在文档中插入一个版权符号&lt;code class=&quot;highlighter-rouge&quot;&gt; ©&lt;/code&gt;，你可以这样写：&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;amp;copy; Markdown 会保留它不动。而若你写：

AT&amp;amp;T Markdown 就会将它转为：

AT&amp;amp;amp;T 类似的状况也会发生在 &amp;lt; 符号上，因为 Markdown 允许 兼容 HTML ，如果你是把 &amp;lt; 符号作为 HTML 标签的定界符使用，那 Markdown 也不会对它做任何转换，但是如果你写：

4 &amp;lt; 5 Markdown 将会把它转换为：

4 &amp;amp;lt; 5 不过需要注意的是，code 范围内，不论是行内还是区块， &amp;lt; 和 &amp;amp; 两个符号都一定会被转换成 HTML 实体，这项特性让你可以很容易地用 Markdown 写 HTML code （和 HTML 相对而言， HTML 语法中，你要把所有的 &amp;lt; 和 &amp;amp; 都转换为 HTML 实体，才能在 HTML 文件里面写出 HTML code。）
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;区块元素-&quot;&gt;区块元素 &lt;span id=&quot;2&quot;&gt;&lt;/span&gt;&lt;/h2&gt;

&lt;h3 id=&quot;段落和换行-&quot;&gt;段落和换行 &lt;span id=&quot;2.1&quot;&gt;&lt;/span&gt;&lt;/h3&gt;

&lt;p&gt;一个 Markdown 段落是由一个或多个连续的文本行组成，它的前后要有一个以上的空行（空行的定义是显示上看起来像是空的，便会被视为空行。比方说，若某一行只包含空格和制表符，则该行也会被视为空行）。普通段落不该用空格或制表符来缩进。&lt;/p&gt;

&lt;p&gt;「由一个或多个连续的文本行组成」这句话其实暗示了 Markdown 允许段落内的强迫换行（插入换行符），这个特性和其他大部分的 text-to-HTML 格式不一样（包括 Movable Type 的「Convert Line Breaks」选项），其它的格式会把每个换行符都转成&lt;code class=&quot;highlighter-rouge&quot;&gt; &amp;lt;br /&amp;gt;&lt;/code&gt; 标签。&lt;/p&gt;

&lt;p&gt;如果你确实想要依赖 Markdown 来插入 &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;br /&amp;gt;&lt;/code&gt; 标签的话，在插入处先按入两个以上的空格然后回车。&lt;/p&gt;

&lt;p&gt;的确，需要多费点事（多加空格）来产生&lt;code class=&quot;highlighter-rouge&quot;&gt; &amp;lt;br /&amp;gt; &lt;/code&gt;，但是简单地「每个换行都转换为&lt;code class=&quot;highlighter-rouge&quot;&gt; &amp;lt;br /&amp;gt;&lt;/code&gt;」的方法在 Markdown 中并不适合， Markdown 中 email 式的 区块引用 和多段落的 列表 在使用换行来排版的时候，不但更好用，还更方便阅读。&lt;/p&gt;

&lt;h3 id=&quot;标题-&quot;&gt;标题 &lt;span id=&quot;2.2&quot;&gt;&lt;/span&gt;&lt;/h3&gt;

&lt;p&gt;Markdown 支持两种标题的语法，类 &lt;strong&gt;Setext&lt;/strong&gt; 和类 &lt;strong&gt;atx&lt;/strong&gt; 形式。&lt;/p&gt;

&lt;p&gt;类 Setext 形式是用底线的形式，利用 &lt;code class=&quot;highlighter-rouge&quot;&gt;=&lt;/code&gt; （最高阶标题）和 &lt;code class=&quot;highlighter-rouge&quot;&gt;-&lt;/code&gt; （第二阶标题），例如：&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;This is an H1
=============

This is an H2
------------- 任何数量的 `=` 和 `-` 都可以有效果。
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;类 Atx 形式则是在行首插入 1 到 6 个 &lt;code class=&quot;highlighter-rouge&quot;&gt;#&lt;/code&gt; ，对应到标题 1 到 6 阶，例如：&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# 这是 H1

## 这是 H2

###### 这是 H6
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;你可以选择性地「闭合」类 atx 样式的标题，这纯粹只是美观用的，若是觉得这样看起来比较舒适，你就可以在行尾加上 #，而行尾的 # 数量也不用和开头一样（行首的井字符数量决定标题的阶数）：&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# 这是 H1 #

## 这是 H2 ##

### 这是 H3 ######
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;区块引用-blockquotes-&quot;&gt;区块引用 Blockquotes &lt;span id=&quot;2.3&quot;&gt;&lt;/span&gt;&lt;/h3&gt;

&lt;p&gt;Markdown 标记区块引用是使用类似 email 中用 &amp;gt; 的引用方式。如果你还熟悉在 email 信件中的引言部分，你就知道怎么在 Markdown 文件中建立一个区块引用，那会看起来像是你自己先断好行，然后在每行的最前面加上 &amp;gt; ：&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;gt; This is a blockquote with two paragraphs. Lorem ipsum dolor sit amet,
&amp;gt; consectetuer adipiscing elit. Aliquam hendrerit mi posuere lectus.
&amp;gt; Vestibulum enim wisi, viverra nec, fringilla in, laoreet vitae, risus.
&amp;gt; 
&amp;gt; Donec sit amet nisl. Aliquam semper ipsum sit amet velit. Suspendisse
&amp;gt; id sem consectetuer libero luctus adipiscing.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Markdown 也允许你偷懒只在整个段落的第一行最前面加上 &amp;gt; ：&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;gt; This is a blockquote with two paragraphs. Lorem ipsum dolor sit amet,
consectetuer adipiscing elit. Aliquam hendrerit mi posuere lectus.
Vestibulum enim wisi, viverra nec, fringilla in, laoreet vitae, risus.

&amp;gt; Donec sit amet nisl. Aliquam semper ipsum sit amet velit. Suspendisse
id sem consectetuer libero luctus adipiscing.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;区块引用可以嵌套（例如：引用内的引用），只要根据层次加上不同数量的 &amp;gt; ：&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;gt; This is the first level of quoting.
&amp;gt;
&amp;gt; &amp;gt; This is nested blockquote.
&amp;gt;
&amp;gt; Back to the first level.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;引用的区块内也可以使用其他的 Markdown 语法，包括标题、列表、代码区块等：&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;gt; ## 这是一个标题。
&amp;gt; 
&amp;gt; 1.   这是第一行列表项。
&amp;gt; 2.   这是第二行列表项。
&amp;gt; 
&amp;gt; 给出一些例子代码：
&amp;gt; 
&amp;gt;     return shell_exec(&quot;echo $input | $markdown_script&quot;);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;任何像样的文本编辑器都能轻松地建立 email 型的引用。例如在 BBEdit 中，你可以选取文字后然后从选单中选择增加引用阶层。&lt;/p&gt;

&lt;h3 id=&quot;列表-&quot;&gt;列表 &lt;span id=&quot;2.4&quot;&gt;&lt;/span&gt;&lt;/h3&gt;

&lt;p&gt;Markdown 支持有序列表和无序列表。&lt;/p&gt;

&lt;p&gt;无序列表使用星号、加号或是减号作为列表标记：&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;*   Red
*   Green
*   Blue 等同于：

+   Red
+   Green
+   Blue 也等同于：

-   Red
-   Green
-   Blue 有序列表则使用数字接着一个英文句点：

1.  Bird
2.  McHale
3.  Parish 很重要的一点是，你在列表标记上使用的数字并不会影响输出的 HTML 结果，上面的列表所产生的 HTML 标记为：

&amp;lt;ol&amp;gt;
&amp;lt;li&amp;gt;Bird&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;McHale&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;Parish&amp;lt;/li&amp;gt;
&amp;lt;/ol&amp;gt; 如果你的列表标记写成：

1.  Bird
1.  McHale
1.  Parish 或甚至是：

3. Bird
1. McHale
8. Parish 你都会得到完全相同的 HTML 输出。重点在于，你可以让 Markdown 文件的列表数字和输出的结果相同，或是你懒一点，你可以完全不用在意数字的正确性。
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;如果你使用懒惰的写法，建议第一个项目最好还是从 1. 开始，因为 Markdown 未来可能会支持有序列表的 start 属性。&lt;/p&gt;

&lt;p&gt;列表项目标记通常是放在最左边，但是其实也可以缩进，最多 3 个空格，项目标记后面则一定要接着至少一个空格或制表符。&lt;/p&gt;

&lt;p&gt;要让列表看起来更漂亮，你可以把内容用固定的缩进整理好：&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;*   Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
    Aliquam hendrerit mi posuere lectus. Vestibulum enim wisi,
    viverra nec, fringilla in, laoreet vitae, risus.
*   Donec sit amet nisl. Aliquam semper ipsum sit amet velit.
    Suspendisse id sem consectetuer libero luctus adipiscing. 但是如果你懒，那也行：

*   Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
Aliquam hendrerit mi posuere lectus. Vestibulum enim wisi,
viverra nec, fringilla in, laoreet vitae, risus.
*   Donec sit amet nisl. Aliquam semper ipsum sit amet velit.
Suspendisse id sem consectetuer libero luctus adipiscing. 如果列表项目间用空行分开，在输出 HTML 时 Markdown 就会将项目内容用 &amp;lt;p&amp;gt; 标签包起来，举例来说：

*   Bird
*   Magic 会被转换为：

&amp;lt;ul&amp;gt;
&amp;lt;li&amp;gt;Bird&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;Magic&amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt; 但是这个：

*   Bird

*   Magic 会被转换为：

&amp;lt;ul&amp;gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;Bird&amp;lt;/p&amp;gt;&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;Magic&amp;lt;/p&amp;gt;&amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt; 列表项目可以包含多个段落，每个项目下的段落都必须缩进 4 个空格或是 1 个制表符：

1.  This is a list item with two paragraphs. Lorem ipsum dolor
    sit amet, consectetuer adipiscing elit. Aliquam hendrerit
    mi posuere lectus.

    Vestibulum enim wisi, viverra nec, fringilla in, laoreet
    vitae, risus. Donec sit amet nisl. Aliquam semper ipsum
    sit amet velit.

2.  Suspendisse id sem consectetuer libero luctus adipiscing. 如果你每行都有缩进，看起来会看好很多，当然，再次地，如果你很懒惰，Markdown 也允许：

*   This is a list item with two paragraphs.

    This is the second paragraph in the list item. You&#39;re
only required to indent the first line. Lorem ipsum dolor
sit amet, consectetuer adipiscing elit.

*   Another item in the same list. 如果要在列表项目内放进引用，那 &amp;gt; 就需要缩进：

*   A list item with a blockquote:

    &amp;gt; This is a blockquote
    &amp;gt; inside a list item. 如果要放代码区块的话，该区块就需要缩进两次，也就是 8 个空格或是 2 个制表符：

*   一列表项包含一个列表区块：

        &amp;lt;代码写在这&amp;gt; 当然，项目列表很可能会不小心产生，像是下面这样的写法：

1986. What a great season. 换句话说，也就是在行首出现数字-句点-空白，要避免这样的状况，你可以在句点前面加上反斜杠。

1986\. What a great season. 代码区块
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;和程序相关的写作或是标签语言原始码通常会有已经排版好的代码区块，通常这些区块我们并不希望它以一般段落文件的方式去排版，而是照原来的样子显示，Markdown 会用 &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;pre&amp;gt;&lt;/code&gt; 和 &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;code&amp;gt;&lt;/code&gt; 标签来把代码区块包起来。&lt;/p&gt;

&lt;p&gt;要在 Markdown 中建立代码区块很简单，只要简单地缩进 4 个空格或是 1 个制表符就可以，例如，下面的输入：&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;这是一个普通段落：

    这是一个代码区块。 Markdown 会转换成：

&amp;lt;p&amp;gt;这是一个普通段落：&amp;lt;/p&amp;gt;

&amp;lt;pre&amp;gt;&amp;lt;code&amp;gt;这是一个代码区块。
&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt; 这个每行一阶的缩进（4 个空格或是 1 个制表符），都会被移除，例如：

Here is an example of AppleScript:

    tell application &quot;Foo&quot;
        beep
    end tell 会被转换为：

&amp;lt;p&amp;gt;Here is an example of AppleScript:&amp;lt;/p&amp;gt;

&amp;lt;pre&amp;gt;&amp;lt;code&amp;gt;tell application &quot;Foo&quot;
    beep
end tell
&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt; 一个代码区块会一直持续到没有缩进的那一行（或是文件结尾）。
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;在代码区块里面， &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;amp;&lt;/code&gt; 、 &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;&lt;/code&gt; 和 &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;gt;&lt;/code&gt; 会自动转成 HTML 实体，这样的方式让你非常容易使用 Markdown 插入范例用的 HTML 原始码，只需要复制贴上，再加上缩进就可以了，剩下的 Markdown 都会帮你处理，例如：&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;lt;div class=&quot;footer&quot;&amp;gt;
    &amp;amp;copy; 2004 Foo Corporation
&amp;lt;/div&amp;gt; 会被转换为：

&amp;lt;pre&amp;gt;&amp;lt;code&amp;gt;&amp;amp;lt;div class=&quot;footer&quot;&amp;amp;gt;
    &amp;amp;amp;copy; 2004 Foo Corporation
&amp;amp;lt;/div&amp;amp;gt;
&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt; 代码区块中，一般的 Markdown 语法不会被转换，像是星号便只是星号，这表示你可以很容易地以 Markdown 语法撰写 Markdown 语法相关的文件。
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;分隔线-&quot;&gt;分隔线 &lt;span id=&quot;2.5&quot;&gt;&lt;/span&gt;&lt;/h3&gt;

&lt;p&gt;你可以在一行中用三个以上的星号、减号、底线来建立一个分隔线，行内不能有其他东西。你也可以在星号或是减号中间插入空格。下面每种写法都可以建立分隔线：&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;* * *

***

*****

- - -

---------------------------------------
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;hr /&gt;
&lt;p&gt;##区段元素 &lt;span id=&quot;3&quot;&gt;&lt;/span&gt;&lt;/p&gt;

&lt;h3 id=&quot;链接-&quot;&gt;链接 &lt;span id=&quot;3.1&quot;&gt;&lt;/span&gt;&lt;/h3&gt;

&lt;p&gt;Markdown 支持两种形式的链接语法： _行内式_和_参考式_两种形式。&lt;/p&gt;

&lt;p&gt;不管是哪一种，链接文字都是用 [方括号] 来标记。&lt;/p&gt;

&lt;p&gt;要建立一个行内式的链接，只要在方块括号后面紧接着圆括号并插入网址链接即可，如果你还想要加上链接的 title 文字，只要在网址后面，用双引号把 title 文字包起来即可，例如：&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;This is [an example](http://example.com/ &quot;Title&quot;) inline link.

[This link](http://example.net/) has no title attribute. 会产生：

&amp;lt;p&amp;gt;This is &amp;lt;a href=&quot;http://example.com/&quot; title=&quot;Title&quot;&amp;gt;
an example&amp;lt;/a&amp;gt; inline link.&amp;lt;/p&amp;gt;

&amp;lt;p&amp;gt;&amp;lt;a href=&quot;http://example.net/&quot;&amp;gt;This link&amp;lt;/a&amp;gt; has no
title attribute.&amp;lt;/p&amp;gt; 如果你是要链接到同样主机的资源，你可以使用相对路径：

See my [About](/about/) page for details. 参考式的链接是在链接文字的括号后面再接上另一个方括号，而在第二个方括号里面要填入用以辨识链接的标记：

This is [an example][id] reference-style link. 你也可以选择性地在两个方括号中间加上一个空格：

This is [an example] [id] reference-style link. 接着，在文件的任意处，你可以把这个标记的链接内容定义出来：

[id]: http://example.com/  &quot;Optional Title Here&quot; 链接内容定义的形式为：
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;方括号（前面可以选择性地加上至多三个空格来缩进），里面输入链接文字
接着一个冒号
接着一个以上的空格或制表符
接着链接的网址
选择性地接着 title 内容，可以用单引号、双引号或是括弧包着
下面这三种链接的定义都是相同：&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;[foo]: http://example.com/  &quot;Optional Title Here&quot;
[foo]: http://example.com/  &#39;Optional Title Here&#39;
[foo]: http://example.com/  (Optional Title Here) 请注意：有一个已知的问题是 Markdown.pl 1.0.1 会忽略单引号包起来的链接 title。
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;链接网址也可以用尖括号包起来：&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;[id]: &amp;lt;http://example.com/&amp;gt;  &quot;Optional Title Here&quot; 你也可以把 title 属性放到下一行，也可以加一些缩进，若网址太长的话，这样会比较好看：

[id]: http://example.com/longish/path/to/resource/here
    &quot;Optional Title Here&quot; 网址定义只有在产生链接的时候用到，并不会直接出现在文件之中。
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;链接辨别标签可以有字母、数字、空白和标点符号，但是并不区分大小写，因此下面两个链接是一样的：&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;[link text][a]
[link text][A] 隐式链接标记功能让你可以省略指定链接标记，这种情形下，链接标记会视为等同于链接文字，要用隐式链接标记只要在链接文字后面加上一个空的方括号，如果你要让 &quot;Google&quot; 链接到 google.com，你可以简化成：

[Google][] 然后定义链接内容：

[Google]: http://google.com/ 由于链接文字可能包含空白，所以这种简化型的标记内也许包含多个单词：

Visit [Daring Fireball][] for more information. 然后接着定义链接：

[Daring Fireball]: http://daringfireball.net/ 链接的定义可以放在文件中的任何一个地方，我比较偏好直接放在链接出现段落的后面，你也可以把它放在文件最后面，就像是注解一样。
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;下面是一个参考式链接的范例：&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;I get 10 times more traffic from [Google] [1] than from
[Yahoo] [2] or [MSN] [3].

  [1]: http://google.com/        &quot;Google&quot;
  [2]: http://search.yahoo.com/  &quot;Yahoo Search&quot;
  [3]: http://search.msn.com/    &quot;MSN Search&quot; 如果改成用链接名称的方式写：

I get 10 times more traffic from [Google][] than from
[Yahoo][] or [MSN][].

  [google]: http://google.com/        &quot;Google&quot;
  [yahoo]:  http://search.yahoo.com/  &quot;Yahoo Search&quot;
  [msn]:    http://search.msn.com/    &quot;MSN Search&quot; 上面两种写法都会产生下面的 HTML。

&amp;lt;p&amp;gt;I get 10 times more traffic from &amp;lt;a href=&quot;http://google.com/&quot;
title=&quot;Google&quot;&amp;gt;Google&amp;lt;/a&amp;gt; than from
&amp;lt;a href=&quot;http://search.yahoo.com/&quot; title=&quot;Yahoo Search&quot;&amp;gt;Yahoo&amp;lt;/a&amp;gt;
or &amp;lt;a href=&quot;http://search.msn.com/&quot; title=&quot;MSN Search&quot;&amp;gt;MSN&amp;lt;/a&amp;gt;.&amp;lt;/p&amp;gt; 下面是用行内式写的同样一段内容的 Markdown 文件，提供作为比较之用：

I get 10 times more traffic from [Google](http://google.com/ &quot;Google&quot;)
than from [Yahoo](http://search.yahoo.com/ &quot;Yahoo Search&quot;) or
[MSN](http://search.msn.com/ &quot;MSN Search&quot;). 参考式的链接其实重点不在于它比较好写，而是它比较好读，比较一下上面的范例，使用参考式的文章本身只有 81 个字符，但是用行内形式的却会增加到 176 个字元，如果是用纯 HTML 格式来写，会有 234 个字元，在 HTML 格式中，标签比文本还要多。
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;使用 Markdown 的参考式链接，可以让文件更像是浏览器最后产生的结果，让你可以把一些标记相关的元数据移到段落文字之外，你就可以增加链接而不让文章的阅读感觉被打断。&lt;/p&gt;

&lt;h3 id=&quot;强调-&quot;&gt;强调 &lt;span id=&quot;3.2&quot;&gt;&lt;/span&gt;&lt;/h3&gt;

&lt;p&gt;Markdown 使用星号（&lt;code class=&quot;highlighter-rouge&quot;&gt;*&lt;/code&gt;）和底线（&lt;code class=&quot;highlighter-rouge&quot;&gt;_&lt;/code&gt;）作为标记强调字词的符号，被 &lt;code class=&quot;highlighter-rouge&quot;&gt;*&lt;/code&gt; 或 &lt;code class=&quot;highlighter-rouge&quot;&gt;_&lt;/code&gt; 包围的字词会被转成用 &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;em&amp;gt;&lt;/code&gt; 标签包围，用两个 &lt;code class=&quot;highlighter-rouge&quot;&gt;*&lt;/code&gt; 或 &lt;code class=&quot;highlighter-rouge&quot;&gt;_&lt;/code&gt; 包起来的话，则会被转成 &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;strong&amp;gt;&lt;/code&gt;，例如：&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;*single asterisks*

_single underscores_

**double asterisks**

__double underscores__ 会转成：

&amp;lt;em&amp;gt;single asterisks&amp;lt;/em&amp;gt;

&amp;lt;em&amp;gt;single underscores&amp;lt;/em&amp;gt;

&amp;lt;strong&amp;gt;double asterisks&amp;lt;/strong&amp;gt;

&amp;lt;strong&amp;gt;double underscores&amp;lt;/strong&amp;gt; 你可以随便用你喜欢的样式，唯一的限制是，你用什么符号开启标签，就要用什么符号结束。
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;强调也可以直接插在文字中间：&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;un*frigging*believable 但是如果你的 * 和 _ 两边都有空白的话，它们就只会被当成普通的符号。
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;如果要在文字前后直接插入普通的星号或底线，你可以用反斜线：&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;\*this text is surrounded by literal asterisks\*
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;代码-&quot;&gt;代码 &lt;span id=&quot;3.3&quot;&gt;&lt;/span&gt;&lt;/h3&gt;

&lt;p&gt;如果要标记一小段行内代码，你可以用反引号把它包起来（`），例如：&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Use the `printf()` function. 会产生：

&amp;lt;p&amp;gt;Use the &amp;lt;code&amp;gt;printf()&amp;lt;/code&amp;gt; function.&amp;lt;/p&amp;gt; 如果要在代码区段内插入反引号，你可以用多个反引号来开启和结束代码区段：

``There is a literal backtick (`) here.`` 这段语法会产生：

&amp;lt;p&amp;gt;&amp;lt;code&amp;gt;There is a literal backtick (`) here.&amp;lt;/code&amp;gt;&amp;lt;/p&amp;gt; 代码区段的起始和结束端都可以放入一个空白，起始端后面一个，结束端前面一个，这样你就可以在区段的一开始就插入反引号：

A single backtick in a code span: `` ` ``

A backtick-delimited string in a code span: `` `foo` `` 会产生：

&amp;lt;p&amp;gt;A single backtick in a code span: &amp;lt;code&amp;gt;`&amp;lt;/code&amp;gt;&amp;lt;/p&amp;gt;

&amp;lt;p&amp;gt;A backtick-delimited string in a code span: &amp;lt;code&amp;gt;`foo`&amp;lt;/code&amp;gt;&amp;lt;/p&amp;gt; 在代码区段内，&amp;amp; 和尖括号都会被自动地转成 HTML 实体，这使得插入 HTML 原始码变得很容易，Markdown 会把下面这段：

Please don&#39;t use any `&amp;lt;blink&amp;gt;` tags. 转为：

&amp;lt;p&amp;gt;Please don&#39;t use any &amp;lt;code&amp;gt;&amp;amp;lt;blink&amp;amp;gt;&amp;lt;/code&amp;gt; tags.&amp;lt;/p&amp;gt; 你也可以这样写：

`&amp;amp;#8212;` is the decimal-encoded equivalent of `&amp;amp;mdash;`. 以产生：

&amp;lt;p&amp;gt;&amp;lt;code&amp;gt;&amp;amp;amp;#8212;&amp;lt;/code&amp;gt; is the decimal-encoded
equivalent of &amp;lt;code&amp;gt;&amp;amp;amp;mdash;&amp;lt;/code&amp;gt;.&amp;lt;/p&amp;gt; 图片
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;很明显地，要在纯文字应用中设计一个「自然」的语法来插入图片是有一定难度的。&lt;/p&gt;

&lt;p&gt;Markdown 使用一种和链接很相似的语法来标记图片，同样也允许两种样式： &lt;em&gt;行内式&lt;/em&gt;和&lt;em&gt;参考式&lt;/em&gt;。&lt;/p&gt;

&lt;p&gt;行内式的图片语法看起来像是：&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;![Alt text](/path/to/img.jpg)

![Alt text](/path/to/img.jpg &quot;Optional title&quot;) 详细叙述如下：
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;一个惊叹号 &lt;code class=&quot;highlighter-rouge&quot;&gt;!&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;接着一个方括号，里面放上图片的替代文字&lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;接着一个普通括号，里面放上图片的网址，最后还可以用引号包住并加上 选择性的 ‘title’ 文字。
参考式的图片语法则长得像这样：&lt;/p&gt;

    &lt;p&gt;&lt;img src=&quot;url/to/image&quot; alt=&quot;Alt text&quot; title=&quot;Optional title attribute&quot; /&gt;
「id」是图片参考的名称，图片参考的定义方式则和连结参考一样：&lt;/p&gt;

    &lt;p&gt;到目前为止， Markdown 还没有办法指定图片的宽高，如果你需要的话，你可以使用普通的 &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;img&amp;gt;&lt;/code&gt; 标签。&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;其它-&quot;&gt;其它 &lt;span id=&quot;4&quot;&gt;&lt;/span&gt;&lt;/h2&gt;

&lt;h3 id=&quot;自动链接-&quot;&gt;自动链接 &lt;span id=&quot;4.1&quot;&gt;&lt;/span&gt;&lt;/h3&gt;

&lt;p&gt;Markdown 支持以比较简短的自动链接形式来处理网址和电子邮件信箱，只要是用尖括号包起来， Markdown 就会自动把它转成链接。一般网址的链接文字就和链接地址一样，例如：&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;lt;http://example.com/&amp;gt; Markdown 会转为：

&amp;lt;a href=&quot;http://example.com/&quot;&amp;gt;http://example.com/&amp;lt;/a&amp;gt; 邮址的自动链接也很类似，只是 Markdown 会先做一个编码转换的过程，把文字字符转成 16 进位码的 HTML 实体，这样的格式可以糊弄一些不好的邮址收集机器人，例如：

&amp;lt;address@example.com&amp;gt; Markdown 会转成：

&amp;lt;a href=&quot;&amp;amp;#x6D;&amp;amp;#x61;i&amp;amp;#x6C;&amp;amp;#x74;&amp;amp;#x6F;:&amp;amp;#x61;&amp;amp;#x64;&amp;amp;#x64;&amp;amp;#x72;&amp;amp;#x65;
&amp;amp;#115;&amp;amp;#115;&amp;amp;#64;&amp;amp;#101;&amp;amp;#120;&amp;amp;#x61;&amp;amp;#109;&amp;amp;#x70;&amp;amp;#x6C;e&amp;amp;#x2E;&amp;amp;#99;&amp;amp;#111;
&amp;amp;#109;&quot;&amp;gt;&amp;amp;#x61;&amp;amp;#x64;&amp;amp;#x64;&amp;amp;#x72;&amp;amp;#x65;&amp;amp;#115;&amp;amp;#115;&amp;amp;#64;&amp;amp;#101;&amp;amp;#120;&amp;amp;#x61;
&amp;amp;#109;&amp;amp;#x70;&amp;amp;#x6C;e&amp;amp;#x2E;&amp;amp;#99;&amp;amp;#111;&amp;amp;#109;&amp;lt;/a&amp;gt; 在浏览器里面，这段字串（其实是 `&amp;lt;a href=&quot;mailto:address@example.com&quot;&amp;gt;address@example.com&amp;lt;/a&amp;gt;`）会变成一个可以点击的「address@example.com」链接。
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;（这种作法虽然可以糊弄不少的机器人，但并不能全部挡下来，不过总比什么都不做好些。不管怎样，公开你的信箱终究会引来广告信件的。）&lt;/p&gt;

&lt;h3 id=&quot;反斜杠-&quot;&gt;反斜杠 &lt;span id=&quot;4.2&quot;&gt;&lt;/span&gt;&lt;/h3&gt;

&lt;p&gt;Markdown 可以利用反斜杠来插入一些在语法中有其它意义的符号，例如：如果你想要用星号加在文字旁边的方式来做出强调效果（但不用 &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;em&amp;gt;&lt;/code&gt; 标签），你可以在星号的前面加上反斜杠：&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;\*literal asterisks\* Markdown 支持以下这些符号前面加上反斜杠来帮助插入普通的符号：

\   反斜线
`   反引号
*   星号
_   底线
{}  花括号
[]  方括号
()  括弧
#   井字号
+   加号
-   减号
.   英文句点
!   惊叹号
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;返回顶部&quot;&gt;&lt;strong&gt;返回&lt;a href=&quot;#home&quot;&gt;顶部&lt;/a&gt;&lt;/strong&gt;&lt;/h2&gt;

&lt;hr /&gt;


            </description>
            <pubDate>Wed, 28 Mar 2018 00:00:00 +0000</pubDate>
            <guid>
                https://knightyun.github.io/2018/03/28/markdown-total
            </guid>
        </item>
        
        <item>
            <title>Markdown简明教程</title>
            <link>
                https://knightyun.github.io/2018/03/27/Markdown-tutorial
            </link>
            <description>
                &lt;hr /&gt;

&lt;h2 id=&quot;目录-&quot;&gt;目录 &lt;span id=&quot;home&quot;&gt;&lt;/span&gt;&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;&lt;a href=&quot;#1&quot;&gt;Markdown简明语法&lt;/a&gt;&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;strong&gt;&lt;a href=&quot;#1.1&quot;&gt;Markdown及扩展&lt;/a&gt;&lt;/strong&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;strong&gt;&lt;a href=&quot;#1.1.1&quot;&gt;表格&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
          &lt;li&gt;&lt;strong&gt;&lt;a href=&quot;#1.1.2&quot;&gt;插入图片&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
          &lt;li&gt;&lt;strong&gt;&lt;a href=&quot;#1.1.3&quot;&gt;定义列表&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
          &lt;li&gt;&lt;strong&gt;&lt;a href=&quot;#1.1.4&quot;&gt;代码块&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
          &lt;li&gt;&lt;strong&gt;&lt;a href=&quot;#1.1.5&quot;&gt;脚注&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
          &lt;li&gt;&lt;strong&gt;&lt;a href=&quot;#1.1.6&quot;&gt;数学公式&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;&lt;a href=&quot;#1.2&quot;&gt;浏览器兼容&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;&lt;a href=&quot;#1.3&quot;&gt;常用Markdown编辑器推荐&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;markdown简明语法&quot;&gt;Markdown简明语法&lt;span id=&quot;1&quot;&gt;&lt;/span&gt;&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Markdown和扩展Markdown简洁的语法&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;代码块高亮&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;图片链接和图片上传&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;&lt;em&gt;LaTex&lt;/em&gt;数学公式&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;离线写博客&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;导入导出Markdown文件&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;markdown及扩展-&quot;&gt;Markdown及扩展 &lt;span id=&quot;1.1&quot;&gt;&lt;/span&gt;&lt;/h3&gt;

&lt;blockquote&gt;
  &lt;p&gt;Markdown 是一种轻量级标记语言，它允许人们使用易读易写的纯文本格式编写文档，然后转换成格式丰富的HTML页面。    —— &lt;a href=&quot;https://zh.wikipedia.org/wiki/Markdown&quot; target=&quot;_blank&quot;&gt; [ 维基百科 ]&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;使用简单的符号标识不同的标题，将某些文字标记为&lt;strong&gt;粗体&lt;/strong&gt;或者&lt;em&gt;斜体&lt;/em&gt;，创建一个&lt;a href=&quot;http://www.csdn.net&quot;&gt;链接&lt;/a&gt;等，详细语法参考帮助？。&lt;/p&gt;

&lt;p&gt;本编辑器支持 &lt;strong&gt;Markdown Extra&lt;/strong&gt; , 　扩展了很多好用的功能。具体请参考&lt;a href=&quot;https://github.com/jmcmanus/pagedown-extra&quot; title=&quot;Pagedown Extra&quot;&gt;Github&lt;/a&gt;.&lt;/p&gt;

&lt;h4 id=&quot;表格-&quot;&gt;表格 &lt;span id=&quot;1.1.1&quot;&gt;&lt;/span&gt;&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Markdown　Extra&lt;/strong&gt;　表格语法：&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;项目&lt;/th&gt;
      &lt;th&gt;价格&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Computer&lt;/td&gt;
      &lt;td&gt;$1600&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Phone&lt;/td&gt;
      &lt;td&gt;$12&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Pipe&lt;/td&gt;
      &lt;td&gt;$1&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;可以使用冒号来定义对齐方式：&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;项目&lt;/th&gt;
      &lt;th style=&quot;text-align: right&quot;&gt;价格&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;数量&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Computer&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1600 元&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;5&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Phone&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;12 元&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;12&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Pipe&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1 元&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;234&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h4 id=&quot;插入图片-&quot;&gt;插入图片 &lt;span id=&quot;1.1.2&quot;&gt;&lt;/span&gt;&lt;/h4&gt;
&lt;blockquote&gt;
  &lt;p&gt;格式一：&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class=&quot;language-markdown highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt; !&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;picture-name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;](&lt;/span&gt;&lt;span class=&quot;sx&quot;&gt;http://xxx.com/xxx.png&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;blockquote&gt;
  &lt;p&gt;格式二：(方便设置图片尺寸)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class=&quot;language-html highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;img&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;src=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;http://xxx.com/xxx.png&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;alt=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;download-failed&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;width=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;XXXpx&quot;&lt;/span&gt;  &lt;span class=&quot;na&quot;&gt;height=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;XXXpx&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;定义列表-&quot;&gt;定义列表 &lt;span id=&quot;1.1.3&quot;&gt;&lt;/span&gt;&lt;/h4&gt;

&lt;dl&gt;
  &lt;dt&gt;&lt;strong&gt;Markdown　Extra&lt;/strong&gt;　定义列表语法：&lt;/dt&gt;
  &lt;dt&gt;项目１&lt;/dt&gt;
  &lt;dt&gt;项目２&lt;/dt&gt;
  &lt;dd&gt;定义 A&lt;/dd&gt;
  &lt;dd&gt;定义 B&lt;/dd&gt;
  &lt;dt&gt;项目３&lt;/dt&gt;
  &lt;dd&gt;定义 C&lt;/dd&gt;
  &lt;dd&gt;
    &lt;p&gt;定义 D&lt;/p&gt;

    &lt;blockquote&gt;
      &lt;p&gt;定义D内容&lt;/p&gt;
    &lt;/blockquote&gt;
  &lt;/dd&gt;
&lt;/dl&gt;

&lt;h4 id=&quot;代码块-&quot;&gt;代码块 &lt;span id=&quot;1.1.4&quot;&gt;&lt;/span&gt;&lt;/h4&gt;
&lt;p&gt;代码块语法遵循标准markdown代码，例如：&lt;/p&gt;
&lt;blockquote&gt;
  &lt;p&gt;python:&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nd&quot;&gt;@requires_authorization&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;somefunc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;param1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&#39;&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;param2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;&#39;&#39;&#39;A docstring&#39;&#39;&#39;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;param1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;param2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;c&quot;&gt;# interesting&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&#39;Greater&#39;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;param2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;param1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;or&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;SomeClass&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;pass&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;message&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&#39;&#39;&#39;interpreter
... prompt&#39;&#39;&#39;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;blockquote&gt;
  &lt;p&gt;c语言:&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;
&lt;span class=&quot;cp&quot;&gt;#include &amp;lt;stdio.h&amp;gt;
&lt;/span&gt;	&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Hello world!&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;脚注--&quot;&gt;脚注  &lt;span id=&quot;1.1.5&quot;&gt;&lt;/span&gt;&lt;/h4&gt;

&lt;p&gt;生成一个脚注[^footnote].
  [^footnote]: 这里是 &lt;strong&gt;脚注&lt;/strong&gt; 的 &lt;em&gt;内容&lt;/em&gt;.&lt;/p&gt;

&lt;h4 id=&quot;数学公式--&quot;&gt;数学公式  &lt;span id=&quot;1.1.6&quot;&gt;&lt;/span&gt;&lt;/h4&gt;
&lt;p&gt;使用MathJax渲染&lt;em&gt;LaTex&lt;/em&gt; 数学公式，详见&lt;a href=&quot;http://math.stackexchange.com/&quot;&gt;math.stackexchange.com&lt;/a&gt;.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;行内公式，数学公式为：$\Gamma(n) = (n-1)!\quad\forall n\in\mathbb N$。&lt;/li&gt;
  &lt;li&gt;块级公式：&lt;/li&gt;
&lt;/ul&gt;

&lt;script type=&quot;math/tex; mode=display&quot;&gt;x = \dfrac{-b \pm \sqrt{b^2 - 4ac}}{2a}&lt;/script&gt;

&lt;p&gt;更多LaTex语法请参考 &lt;a href=&quot;http://meta.math.stackexchange.com/questions/5020/mathjax-basic-tutorial-and-quick-reference&quot;&gt;这儿&lt;/a&gt;.&lt;/p&gt;

&lt;h3 id=&quot;浏览器兼容-&quot;&gt;浏览器兼容 &lt;span id=&quot;1.2&quot;&gt;&lt;/span&gt;&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;目前，本编辑器对Chrome浏览器支持最为完整。建议大家使用较新版本的Chrome。&lt;/li&gt;
  &lt;li&gt;IE９以下不支持&lt;/li&gt;
  &lt;li&gt;IE９，１０，１１存在以下问题
    &lt;ol&gt;
      &lt;li&gt;不支持离线功能&lt;/li&gt;
      &lt;li&gt;IE9不支持文件导入导出&lt;/li&gt;
      &lt;li&gt;IE10不支持拖拽文件导入&lt;/li&gt;
    &lt;/ol&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;常用markdown编辑器推荐-&quot;&gt;常用Markdown编辑器推荐 &lt;span id=&quot;1.3&quot;&gt;&lt;/span&gt;&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Markdownpad&lt;/strong&gt;： 详情请点击 &lt;a href=&quot;http://markdownpad.com/&quot;&gt;官网&lt;/a&gt;。
（貌似专业版需要收取一定dollars$，需要序列号自行baidu。）&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Markpad&lt;/strong&gt;：详情前往 &lt;a href=&quot;http://markpad.fluid.impa.br/&quot;&gt;官网&lt;/a&gt;。
（推荐使用，Microsoft Store也有，完全免费，支持及时效果浏览。）&lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;CSDN博客编辑器&lt;/strong&gt;：CSDN网站内置编辑器。（这篇文件就是这样写出来的-_-)&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Harropad&lt;/strong&gt;: &lt;a href=&quot;http://pad.haroopress.com/user.html&quot;&gt;官网&lt;/a&gt;&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Retext&lt;/strong&gt;: 简单强大的文本编辑器，可控制输出格式：pdf, html等，仅支持Linux（推荐）.&lt;a href=&quot;https://github.com/retext-project/retext&quot;&gt;下载&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;返回顶部&quot;&gt;&lt;strong&gt;返回&lt;a href=&quot;#home&quot;&gt;顶部&lt;/a&gt;&lt;/strong&gt;&lt;/h2&gt;

&lt;hr /&gt;


            </description>
            <pubDate>Tue, 27 Mar 2018 00:00:00 +0000</pubDate>
            <guid>
                https://knightyun.github.io/2018/03/27/Markdown-tutorial
            </guid>
        </item>
        
    </channel>
</rss>