统计一个页面用到的html,css,js

html 复制代码
<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>统计html</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            user-select: none;
        }

        body {
            background: #0b1b2c;
        }

        #app {
            display: flex;
        }

        #textareaCount {
            margin: 5px 100px;
        }

        div {
            color: aliceblue;
        }

        div li {
            list-style: none;
            margin: 5px;
        }
    </style>
</head>

<body>
    <div id="app">
        <textarea id="textareaCount" class="textarea1" name="textareaCSS" rows="8" cols="50"
            placeholder="在这里输入HTML内容"></textarea>
        <dl>
            <dt id="countHTML">HTML标签数量:0。</dt>
            <dt>用到的标签有:</dt>
            <dd id="appHTML"></dd>
        </dl>
        <dl>
            <dt id="countCSS">CSS属性数量: 0。</dt>
            <dt>用到的属性有:</dt>
            <dd id="appCSS"></dd>
            
        </dl>
        <dl>
            <dt id="countJS">JS函数和变量数量: 0。</dt>
            <dt>用到的函数和变量有:</dt>
            <dd id="appJS"></dd>
        </dl>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', function () {
            document.getElementById('textareaCount').addEventListener('input', function () {
                const textareaCount = this.value;

                // 正则表达式匹配所有 HTML 标签
                const tagRegex = /<(\w+)[^>]*>/g;
                const htmlTags = textareaCount.match(tagRegex) || [];
              console.log(htmlTags);
                document.getElementById('countHTML').innerText = `HTML标签数量: ${htmlTags.length}。`;

                const tags = [];

                let match;
                while ((match = tagRegex.exec(textareaCount)) !== null) {
                    tags.push(match[1]);
                }

                // 将数组去重并排序
                const tagList = Array.from(new Set(tags)).sort();

                const appHTML = document.getElementById('appHTML');
                const ulHTML = document.createElement('ul');
                appHTML.innerHTML = ''; // 清空之前的列表
                appHTML.appendChild(ulHTML);

                tagList.forEach((tag, index) => {
                    const li = document.createElement('li');
                    li.textContent = `${index + 1}. ${tag}`;
                    ulHTML.appendChild(li);
                });

                // 正则表达式匹配所有 CSS 属性
                const propertyRegex = /([a-zA-Z-]+)\s*:/g;
                const cssProperties = textareaCount.match(propertyRegex) || [];
                document.getElementById('countCSS').innerText = `CSS属性数量: ${cssProperties.length}。`;
                const properties = [];

                while ((match = propertyRegex.exec(textareaCount)) !== null) {
                    properties.push(match[1]);
                }

                // 将数组去重并排序
                const propertyList = Array.from(new Set(properties)).sort();

                const appCSS = document.getElementById('appCSS');
                const ulCSS = document.createElement('ul');
                appCSS.innerHTML = ''; // 清空之前的列表
                appCSS.appendChild(ulCSS);

                propertyList.forEach((property, index) => {
                    const li = document.createElement('li');
                    li.textContent = `${index + 1}. ${property}`;
                    ulCSS.appendChild(li);
                });
                console.log(properties);

                // 正则表达式匹配所有 JS 关键字
                const jsWordRegex = /(function\s+\w+|var\s+\w+|let\s+\w+|const\s+\w+)/g;
                const jsFunctions = textareaCount.match(jsWordRegex) || [];
                document.getElementById('countJS').innerText = `JS函数和变量数量: ${jsFunctions.length}。`;

                const jsWords = [];

                while ((match = jsWordRegex.exec(textareaCount)) !== null) {
                    jsWords.push(match[0]);
                }

                // 将数组去重并排序
                const sortedJsWords = Array.from(new Set(jsWords)).sort();
                console.log(sortedJsWords);

                const appJS = document.getElementById('appJS');
                const ulJS = document.createElement('ul');
                appJS.innerHTML = ''; // 清空之前的列表
                appJS.appendChild(ulJS);

                sortedJsWords.forEach((word, index) => {
                    const li = document.createElement('li');
                    li.textContent = `${index + 1}. ${word}`;
                    ulJS.appendChild(li);
                });
            });
        });
    </script>
</body>

</html>
相关推荐
蓝婷儿38 分钟前
每天一个前端小知识 Day 28 - Web Workers / 多线程模型在前端中的应用实践
前端
琹箐38 分钟前
Ant ASpin自定义 indicator 报错
前端·javascript·typescript
小小小小小惠42 分钟前
Responsetype blob会把接口接收的二进制文件转换成blob格式
前端·javascript
爱电摇的小码农43 分钟前
【深度探究系列(5)】:前端开发打怪升级指南:从踩坑到封神的解决方案手册
前端·javascript·css·vue.js·node.js·html5·xss
kymjs张涛1 小时前
零一开源|前沿技术周报 #7
android·前端·ios
爱编程的喵1 小时前
React入门实战:从静态渲染到动态状态管理
前端·javascript
Tttian6221 小时前
npm init vue@latestnpm error code ETIMEDOUT
前端·vue.js·npm
患得患失9492 小时前
【前端】【组件库开发】【原理】【无框架开发】现代网页弹窗开发指南:从基础到优化
前端
唐叔在学习2 小时前
不用装插件!3轮对话,我用油猴脚本+AI复刻了掘金闪念笔记,真香!
javascript·浏览器
AliciaIr2 小时前
深入React事件机制:解密“合成事件”与“事件委托”的底层奥秘
javascript·react.js