整合營銷服務商

          電腦端+手機端+微信端=數據同步管理

          免費咨詢熱線:

          JavaScript-如何從一個數組中刪除一個特定的

          JavaScript-如何從一個數組中刪除一個特定的項目?

          rray.prototype.indexOf()

          用indexOf找到你想刪除的數組元素的索引,然后用splice刪除該索引。

          const array=[2, 5, 9];
          
          console.log(array);
          
          const index=array.indexOf(5);
          if (index > -1) {
            array.splice(index, 1);
          }
          
          // array=[2, 9]
          console.log(array); 

          splice的第二個參數是要移除的元素的數量。注意,splice修改了原地的數組,并返回一個包含被移除的元素的新數組。

          為了完整起見,這里有一些函數。第一個函數只刪除單一的出現(即從[2,5,9,1,5,8,5]中刪除5的第一個匹配),而第二個函數則刪除所有出現的情況。

          function removeItemOnce(arr, value) {
            var index=arr.indexOf(value);
            if (index > -1) {
              arr.splice(index, 1);
            }
            return arr;
          }
          
          function removeItemAll(arr, value) {
            var i=0;
            while (i < arr.length) {
              if (arr[i]===value) {
                arr.splice(i, 1);
              } else {
                ++i;
              }
            }
            return arr;
          }
          // Usage
          console.log(removeItemOnce([2,5,9,1,5,8,5], 5))
          console.log(removeItemAll([2,5,9,1,5,8,5], 5))

          在TypeScript中,這些函數可以添加類型參數。

          function removeItem<T>(arr: Array<T>, value: T): Array<T> { 
            const index=arr.indexOf(value);
            if (index > -1) {
              arr.splice(index, 1);
            }
            return arr;
          }


          Array.prototype.filter()

          家好,很高興又見面了,我是"高級前端?進階?",由我帶著大家一起關注前端前沿、深入前端底層技術,大家一起進步,也歡迎大家關注、點贊、收藏、轉發,您的支持是我不斷創作的動力。

          什么是 UnCSS

          UnCSS 是一個從樣式表中刪除未使用的 CSS 的工具,可以跨多個文件工作,并支持 Javascript 注入的 CSS。

          UnCSS 刪除未使用規則的過程如下:

          • HTML 文件由 jsdom 加載并執行 JavaScript。
          • 所有樣式表均由 PostCSS 解析。
          • document.querySelector 過濾掉 HTML 文件中找不到的選擇器。
          • 其余規則將轉換回 CSS。

          但是使用 Uncss 需要注意以下幾點:

          • UnCSS 不能在非 HTML 頁面上運行,例如:模板或 PHP 文件。 如果開發者需要針對模板運行 UnCSS,應該從模板生成示例 HTML 頁面,并在這些生成的文件上運行 uncss; 或者運行一個實時的本地開發服務器,并將 uncss 指向該服務器。
          • UnCSS 僅運行在頁面加載時運行的 Javascript。 它不會(也不能)處理在用戶交互(如按鈕單擊)上運行的 Javascript。 必須使用 ignore 選項來保留 Javascript 在用戶交互時添加的類。

          目前 uncss 在 Github 上通過 MIT 協議開源,有超過 9.3k 的 star,194k 的項目依賴量,是一個值得關注的前端開源項目。

          如何使用 UnCSS

          下面是在 Node.js 環境中使用 uncss 的示例:

          var uncss=require('uncss');
          var files=['my', 'array', 'of', 'HTML', 'files', 'or', 'http://urls.com'],
            options={
              banner: false,
              csspath: '../public/css/',
              htmlroot: 'public',
              ignore: ['#added_at_runtime', /test\-[0-9]+/],
              ignoreSheets: [/fonts.googleapis/],
              inject: function (window) {
                window.document
                  .querySelector('html')
                  .classList.add('no-csscalc', 'csscalc');
              },
              jsdom: {
                userAgent: 'Mozilla/5.0 (iPhone; CPU iPhone OS 10_3 like Mac OS X)',
              },
              media: ['(min-width: 700px) handheld and (orientation: landscape)'],
              raw: 'h1 { color: green }',
              report: false,
              strictSSL: true,
              stylesheets: [
                'lib/bootstrap/dist/css/bootstrap.css',
                'src/public/css/main.css',
              ],
              timeout: 1000,
              uncssrc: '.uncssrc',
              userAgent: 'Mozilla/5.0 (iPhone; CPU iPhone OS 10_3 like Mac OS X)',
            };
          uncss(files, options, function (error, output) {
            console.log(output);
          });
          /* Look Ma, no options! */
          uncss(files, function (error, output) {
            console.log(output);
          });
          /* Specifying raw HTML */
          var rawHtml='...';
          uncss(rawHtml, options, function (error, output) {
            console.log(output);
          });

          值得一提的是,UnCSS 還可以與其他 JavaScript 構建系統結合使用,例如: Grunt、Broccoli 或 Gulp,只需要安裝特定的庫即可。

          • grunt-uncss
          • gulp-uncss
          • broccoli-uncss

          在 CLI 環境中使用命令如下:

          Usage: uncss [options] <file or URL, ...>
              e.g. uncss https://getbootstrap.com/docs/3.3/examples/jumbotron/ > stylesheet.css
          
          Options:
          
            -h, --help                            output usage information
            -V, --version                         output the version number
            -i, --ignore <selector, ...>          Do not remove given selectors
            -m, --media <media_query, ...>        Process additional media queries
            -C, --csspath <path>                  Relative path where the CSS files are located
            -s, --stylesheets <file, ...>         Specify additional stylesheets to process
            -S, --ignoreSheets <selector, ...>    Do not include specified stylesheets
            -r, --raw <string>                    Pass in a raw string of CSS
            -t, --timeout <milliseconds>          Wait for JS evaluation
            -H, --htmlroot <folder>               Absolute paths' root location
            -u, --uncssrc <file>                  Load these options from <file>
            -n, --noBanner                        Disable banner
            -a, --userAgent <string>              Use a custom user agent string
            -I, --inject <file>                   Path to javascript file to be executed before uncss runs
            -o, --output <file>                   Path to write resulting CSS to

          請注意,可以將本地文件路徑(由 glob 處理)和 URL 傳遞給程序。

          • banner(布爾值,默認值:true):是否應在已處理 CSS 中的每個文件塊之前添加banner。
          • csspath(字符串):CSS 文件與 HTML 文件相關的路徑。 默認情況下,UnCSS 使用 <link rel="stylesheet" href="path/to/file.css"/> 中指定的路徑。
          • htmlroot(字符串):項目根目錄所在的位置。 例如,如果 HTML 引用具有根相對 URL 的本地文件,即 href="/css/style.css"則很有用。
          • ignore (string[]):提供不應被 UnCSS 刪除的選擇器列表。 例如,通過用戶與頁面交互(懸停、單擊)添加的樣式,因為 UnCSS 尚無法檢測到這些樣式。 文字名稱和正則表達式模式都可以識別。 否則,可以在特定選擇器之前添加注釋:
          /* uncss:ignore */
          .selector1 {
              /* this rule will be ignored */
          }
          .selector2 {
              /* this will NOT be ignored */
          }
          /* uncss:ignore start */
          /* all rules in here will be ignored */
          /* uncss:ignore end */
          • 支持更多配置,包括:ignoreSheets、inject、jsdom、media、raw、report、strictSSL、stylesheets、timeout、uncssrc等參考文末資料。

          參考資料

          https://github.com/uncss/uncss

          https://m.youtube.com/watch?v=DX7McYRGJ8o

          https://uncss-online.com/

          lt;div class="aa">aa</div>

          <div class="bb">bb</div>

          <div class="aa">aa</div>

          <div class="fuClass"><h4 id="Title">jiajia</h4></div>

          <ul>

          <li><a href="/" class="delete second">11</a></li>

          <li>22</li>

          <li>33</li>

          </ul>

          const lis=document.querySelectorAll("li");

          const list=document.querySelector("ul");

          //第一種刪除方法

          //lis[0].remove();

          //第二種刪除方法

          //list.removeChild(lis[1]);

          //class && attr

          const firstLi=document.querySelector("li:first-child");

          const link=firstLi.children[0];

          //a 類

          let val;

          val=link.className;

          val=link.classList;

          // val=link.classList[0];

          link.classList.add('test');

          link.classList.remove('test');

          //a 屬性

          val=link.getAttribute('href');

          val=link.setAttribute('href','http://www.baidu.com');

          link.setAttribute('title','百度');

          link.removeAttribute('title');

          val=link.hasAttribute('title');

          console.log(val)


          主站蜘蛛池模板: 亚洲一区免费观看| 国模少妇一区二区三区| 日韩美女在线观看一区| 精品一区二区三区四区在线播放| 中文字幕日韩丝袜一区| 久久精品无码一区二区三区不卡 | 亚洲A∨精品一区二区三区| 日本丰满少妇一区二区三区| 99精品国产一区二区三区| 亚洲综合无码一区二区| 日本v片免费一区二区三区| 久久久国产一区二区三区| 蜜桃臀无码内射一区二区三区| 国产午夜福利精品一区二区三区| 无码av免费一区二区三区试看| 伊人色综合网一区二区三区 | 国产无套精品一区二区| 在线观看国产一区二三区| 国产精品一区三区| 日韩中文字幕一区| 国产品无码一区二区三区在线蜜桃 | 另类免费视频一区二区在线观看 | 成人免费视频一区二区三区 | 亚洲一区二区三区亚瑟 | 久久亚洲综合色一区二区三区| 亚洲av无码片vr一区二区三区 | 免费无码一区二区三区| 国产激情一区二区三区四区| 日韩精品成人一区二区三区| 国产精品一区二区综合| 3d动漫精品一区视频在线观看| 丰满人妻一区二区三区视频| 无码av免费毛片一区二区| 国产精品女同一区二区久久 | tom影院亚洲国产一区二区| 伦理一区二区三区| 日本一区二区三区中文字幕| 亚洲国产AV一区二区三区四区 | 一区二区三区免费电影| 久久精品国内一区二区三区| 久久一区二区精品综合|