缩小 html 帮助

发布于 2024-08-11 16:37:24 字数 881 浏览 5 评论 0原文

我有一个包含 2000 个项目的数组,我需要以 html 形式显示它们 - 每个项目都放置在一个 div 中。现在,每个项目都可以有 6 个链接,单击即可执行进一步操作。以下是单个项目当前的外观:

<div class='b'>
  <div class='r'>
    <span id='l1' onclick='doSomething(itemId, linkId);'>1</span>
    <span id='l2' onclick='doSomething(itemId, linkId);'>2</span>
    <span id='l3' onclick='doSomething(itemId, linkId);'>3</span>
    <span id='l4' onclick='doSomething(itemId, linkId);'>4</span>
    <span id='l5' onclick='doSomething(itemId, linkId);'>5</span>
    <span id='l6' onclick='doSomething(itemId, linkId);'>6</span>
  </div>
  <div class='c'>
  some item text
  </div>
</div>

现在的问题在于性能。我正在使用innerHTML 将项目设置到页面上的主div 中。我的“单个项目”包含的 html 越多,DOM 添加它所需的时间就越长。我现在正在尝试减少 HTML 使其尽可能小。有没有办法以不同的方式渲染跨度,而不必为每个跨度使用单个跨度?也许使用 jQuery?

I have an array of 2000 items, that I need to display in html - each of the items is placed into a div. Now each of the items can have 6 links to click on for further action. Here is how a single item currently looks:

<div class='b'>
  <div class='r'>
    <span id='l1' onclick='doSomething(itemId, linkId);'>1</span>
    <span id='l2' onclick='doSomething(itemId, linkId);'>2</span>
    <span id='l3' onclick='doSomething(itemId, linkId);'>3</span>
    <span id='l4' onclick='doSomething(itemId, linkId);'>4</span>
    <span id='l5' onclick='doSomething(itemId, linkId);'>5</span>
    <span id='l6' onclick='doSomething(itemId, linkId);'>6</span>
  </div>
  <div class='c'>
  some item text
  </div>
</div>

Now the problem is with the performance. I am using innerHTML to set the items into a master div on the page. The more html my "single item" contains the longer the DOM takes to add it. I am now trying to reduce the HTML to make it small as possible. Is there a way to render the span's differently without me having to use a single span for each of them? Maybe using jQuery?

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

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

发布评论

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

评论(5

风为裳 2024-08-18 16:37:24

您应该做的第一件事是通过 jQuery 或其他框架将 onclick 事件附加到 DIV 并让它冒泡,以便您可以使用 doSomething 来覆盖所有情况,并且根据您单击的元素,您可以提取项目 ID和链接 ID。另外,跨度真的需要 ID 吗?根据您的示例代码我不知道。另外,也许您可​​以根据需要通过 AJAX 获取它们,而不是在页面加载时加载链接和项目 ID。

午餐吃沙拉时我的两分钱,
nickyt

更新了 vikasde 的信息。这个语法可能不完全正确。我正在午休。

$(".b").bind( // the class of your div, use an ID , e.g. #someID if you have more than one element with class b
    "click",
    function(e) { // e is the event object
        // do something with $(e.target), like check if it's one of your links and then do something with it.
    }
);

First thing you should be doing is attaching the onclick event to the DIV via jQuery or some other framework and let it bubble down so that you can use doSomething to cover all cases and depending on which element you clicked on, you could extract the item ID and link ID. Also do the spans really need IDs? I don't know based on your sample code. Also, maybe instead of loading the link and item IDs on page load, get them via AJAX on a as you need them basis.

My two cents while eating salad for lunch,
nickyt

Update off the top of my head for vikasde . Syntax of this might not be entirely correct. I'm on lunch break.

$(".b").bind( // the class of your div, use an ID , e.g. #someID if you have more than one element with class b
    "click",
    function(e) { // e is the event object
        // do something with $(e.target), like check if it's one of your links and then do something with it.
    }
);
柒七 2024-08-18 16:37:24

如果您设置节点的 InnerHtml 属性,DOM 必须解释您的 HTML 文本并将其转换为节点。本质上,您在这里运行一个语言解释器。更多文本,更多处理时间。我怀疑(但不确定)创建实际的 DOM 元素节点以及所有必需的内容嵌套并将它们挂接到包含节点会更快。您的“InnerHTML”解决方案在幕后做同样的事情,但还需要进行额外的工作来理解您的文本。

我也同意其他人的建议,他说在服务器上构建所有这些内容可能比通过 JS 在客户端构建更经济。

最后,我认为您可以消除 span 的大部分内容。您不需要 ID,也不需要 onclick() 中的参数。调用一个 JS 函数,该函数将确定从哪个节点调用它,向上一个节点查找包含的 div 并可能循环包含的节点和/或查看文本以找出其中的哪一项它应该响应的 div 。您可以让 onclick 处理程序完成大量工作 - 这项工作仅在鼠标单击时完成一次,并且不会乘以 2000 倍。它不会花费可察觉的用户时间。

If you set the InnerHtml property of a node, the DOM has to interpret your HTML text and convert it into nodes. Essentially, you're running a language interpreter here. More text, more processing time. I suspect (but am not sure) that it would be faster to create actual DOM element nodes, with all requisite nesting of contents, and hook those to the containing node. Your "InnerHTML" solution is doing the same thing under the covers but also the additional work of making sense of your text.

I also second the suggestion of someone else who said it might be more economical to build all this content on the server rather than in the client via JS.

Finally, I think you can eliminate much of the content of your spans. You don't need an ID, you don't need arguments in your onclick(). Call a JS function which will figure out which node it's called from, go up one node to find the containing div and perhaps loop down the contained nodes and/or look at the text to figure out which item within a div it should be responding to. You can make the onclick handler do a whole lot of work - this work only gets done once, at mouse click time, and will not be multiplied by 2000x something. It will not take a perceptible amount of user time.

鹿! 2024-08-18 16:37:24

John Resig 在 documentDragments 上撰写了一篇博客 http://ejohn.org/blog /dom-documentfragments/

我的建议是为每一行创建一个 documentDragment 并在创建时将其附加到 DOM。如果浏览器有任何挂起,包装每个 appendChildtimeout 可能会有所帮助,

function addRow(row) {
    var fragment = document.createDocumentFragment();

    var div = document.createElement('div');
    div.addAttribute('class', 'b');

    fragment.appendChild(div);
    div.innerHtml = "<div>what ever you want in each row</div>";

    // setting a timeout of zero will allow the browser to intersperse the action of attaching to the dom with other things so that the delay isn't so noticable
    window.setTimeout(function() { 
        document.body.appendChild(div);
    }, 0);
};

希望有帮助

John Resig wrote a blog on documentDragments http://ejohn.org/blog/dom-documentfragments/

My suggestion is to create a documentDragment for each row and append that to the DOM as you create it. A timeout wrapping each appendChild may help if there is any hanging from the browser

function addRow(row) {
    var fragment = document.createDocumentFragment();

    var div = document.createElement('div');
    div.addAttribute('class', 'b');

    fragment.appendChild(div);
    div.innerHtml = "<div>what ever you want in each row</div>";

    // setting a timeout of zero will allow the browser to intersperse the action of attaching to the dom with other things so that the delay isn't so noticable
    window.setTimeout(function() { 
        document.body.appendChild(div);
    }, 0);
};

hope that helps

似最初 2024-08-18 16:37:24

另一个问题是页面上有太多内容,浏览器无法正常处理。我不确定页面的设计是否允许这样做,但是如何将这 2000 行放入具有固定大小和 overflow: autoDIV 中,以便用户获得可滚动的内容页面中的窗口?

这不是我作为用户所喜欢的,但如果它修复了光标的怪异现象,它可能是一个可以接受的解决方法。

One other problem is that there's too much stuff on the page for your browser to handle gracefully. I'm not sure if the page's design permits this, but how about putting those 2000 lines into a DIV with a fixed size and overflow: auto so the user gets a scrollable window in the page?

It's not what I'd prefer as a user, but if it fixes the cursor weirdness it might be an acceptable workaround.

陌若浮生 2024-08-18 16:37:24

另一种解决方案

...针对“页面上的内容太多”问题:(

当您厌倦了这些建议时请告诉我!)

如果您可以选择使用嵌入式对象,例如 Java Applet(我个人的偏好,但大多数人不会碰它)或 JavaFX 或 Flash 或 Silverlight 或...

然后您可以使用该技术显示所有时髦的数据,并将其嵌入到您的浏览器页面中。该页面的内容与浏览器无关,因此不会让您感到困惑。

除了 Java 或其他的加载时间之外,这对用户来说可能是透明且不可见的,即(几乎)可以这样做,以便文本看起来显示在页面上,就像直接显示在 HTML 中一样。

Yet Another Solution

...to the "too much stuff on the page" problem:

(please let me know when you get sick and tired of these suggestions!)

If you have the option of using an embedded object, say a Java Applet (my personal preference but most people won't touch it) or JavaFX or Flash or Silverlight or...

then you could display all that funky data in that technology, embedded into your browser page. The contents of the page wouldn't be any of the browser's business and hence it wouldn't choke up on you.

Apart from the load time for Java or whatever, this could be transparent and invisible to the user, i.e. it's (almost) possible to do this so the text appears to be displayed on the page just as if it were directly in the HTML.

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