如何在没有弹出框的情况下嵌入精确的DOM元素计数器

发布于 2025-02-05 17:09:05 字数 1433 浏览 3 评论 0 原文

绊倒在JavaScript dom元素计数器计数器在这里在这里(由lingtalfi提供)加快我的网站。显然,DOM元素的长度和深度似乎具有 on speed 。我想计算DOM开销,并在HTML中显示它,而无需键盘。这可以在页面加载以不干扰网站的速度后进行5秒钟。

方法1:警报弹出框

 <a href="
    javascript: (function()
       {alert(document.getElementsByTagName('*').length);
       }());
    ">Count DOM nodes of a page
 </a>

方法2:只需在我的网站上的HTML写入

 <script>
      (function() {
       document.write(document.getElementsByTagName('*').length);
      }());
 </script>

第一个方法弹出814,而第二种方法则写入142。

我的问题是:有没有办法(延迟和)在HTML中输出正确数量的DOM元素数,而无需单击弹出窗口以计算DOM元素?

(function () {document.write(document.getElementsByTagName('*').length); }());
body {
font-size: 5em;
font-family: "Arial";
}

Stumbling upon a Javascript DOM Elements Counter here and here (courtesy of lingtalfi) I want to speed up my website. Apparently the number of, the length of and the depth of DOM elements seems to have an impact on speed. I want to count the DOM overhead and show it in the HTML without a need for a keypress. This could be done 5 seconds after the page has loaded to not interfere with the speed of the website.

Method 1: Alert Popup Box

 <a href="
    javascript: (function()
       {alert(document.getElementsByTagName('*').length);
       }());
    ">Count DOM nodes of a page
 </a>

Method 2: Just Write in HTML

 <script>
      (function() {
       document.write(document.getElementsByTagName('*').length);
      }());
 </script>

On my site the first method popups 814, while the second method writes 142. Quite a difference!

My question is: Is there a way to (delay and) output the correct number of DOM elements just in HTML without the need to click on a popup to count the DOM elements?

(function () {document.write(document.getElementsByTagName('*').length); }());
body {
font-size: 5em;
font-family: "Arial";
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

过期情话 2025-02-12 17:09:05

我绝对不确定您的问题!
您在寻找类似的东西吗?

请记住,演示中的数字可能比代码显示的大。堆栈溢出在场景后面增加了更多元素。

document.addEventListener("DOMContentLoaded", () => {
    setTimeout(() => {
        document.querySelector('aside').textContent = document.getElementsByTagName('*').length;
    }, 5000)
});
<html>
    <head>
    </head>
    <body>
        <div class="header">
            <div class="pages">
                <div class="page">index</div>
                <div class="page">contact</div>
            </div>
        </div>
        <div class="main-content">
            <div class="section-1">

            </div>
            <div class="section-2">

            </div>
            <div class="section-3">

            </div>
        </div>
        <footer>

        </footer>
        <aside></aside>
    </body>
</html>

I am absolutely not sure about your question!
Are you searching for something like that ?

Please keep in mind that the number in the demo may be larger than the code shows. Stack Overflow adds more elements behind the scenes.

document.addEventListener("DOMContentLoaded", () => {
    setTimeout(() => {
        document.querySelector('aside').textContent = document.getElementsByTagName('*').length;
    }, 5000)
});
<html>
    <head>
    </head>
    <body>
        <div class="header">
            <div class="pages">
                <div class="page">index</div>
                <div class="page">contact</div>
            </div>
        </div>
        <div class="main-content">
            <div class="section-1">

            </div>
            <div class="section-2">

            </div>
            <div class="section-3">

            </div>
        </div>
        <footer>

        </footer>
        <aside></aside>
    </body>
</html>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文