有多少元素的ID的浏览器可以安全地性能下降之前办理? [英] How many element id's can browsers safely handle before performance degredation?

查看:103
本文介绍了有多少元素的ID的浏览器可以安全地性能下降之前办理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用元素的ID是JavaScript来'得到'元素最快的方法。有拇指或最佳实践指南的多少这些ID的之一可以期望浏览器的性能开始降解之前,应使用一个规则?

Using element id's is the fastest way for javascript to 'get' an element. Is there a rule of thumb or best practices guideline on how many of these id's should be used before one can expect browser performance to start degrading?

推荐答案

这是ID,就其本身而言,只是一个属性值。唯一的表演问题是多余的比特和字节,浏览器必须下载。从一个JavaScript POV,DOM中的多个元件,时间越长,可以采取遍历它,但是这不直接涉及到您可能使用ID的数目。

An ID, in and of itself, is just an attribute value. The only 'performance' issue is extra bits and bytes the browser has to download. From a JavaScript POV, the more elements in the DOM, the longer it can take to traverse it, but that's not directly related to the number of IDs you may be using.

编辑:

要澄清,如果你的JS是这样的:

To clarify if your JS is this:

document.getElementById("myID")

如果你的HTML看起来像这样也没关系:

it doesn't matter if your HTML looks like this:

<div id="div1">
  <div id="div2">
    ...
      <div id="div999">
        <div id="myDiv">

或本

<div>
  <div>
    ...
      <div>
        <div id="myDiv">

的JS应该运行两个的这些实施例是相同的。

The JS should run the same for both of those examples.

这篇关于有多少元素的ID的浏览器可以安全地性能下降之前办理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆