统计一个页面用到的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>
相关推荐
谎言西西里3 小时前
JS 高手必会:手写 new 与 instanceof
javascript
雪碧聊技术4 小时前
前端项目代码发生改变,如何重新部署到linux服务器?
前端·vue3·centos7·代码更新,重新部署
liulilittle4 小时前
C++ 浮点数封装。
linux·服务器·开发语言·前端·网络·数据库·c++
wordbaby4 小时前
Expo 进阶指南:赋予 TanStack Query “原生感知力” —— 深度解析 AppState 与 NetInfo
前端·react native
Moment5 小时前
从美团全栈化看 AI 冲击:前端转全栈,是自救还是必然 🤔🤔🤔
前端·后端·面试
天问一5 小时前
使用 Vue Router 进行路由定制和调用的示例
前端·javascript·vue.js
韩立学长6 小时前
【开题答辩实录分享】以《基于Vue的非遗文化知识分享平台的设计与实现》为例进行选题答辩实录分享
前端·javascript·vue.js
优弧6 小时前
离开舒适区100天,我后悔了吗?
前端·后端·面试
胡gh6 小时前
css的臂膀,前端动效的利器,还是布局的“隐形陷阱”?
前端·css·html
灵感菇_7 小时前
Flutter Riverpod 完整教程:从入门到实战
前端·flutter·ui·状态管理