出于速度和可读性原因创建新的 XHTML 标记是个好主意吗?

发布于 2024-09-08 20:19:58 字数 1114 浏览 7 评论 0原文

我为一家网络应用程序初创公司进行开发,经常遇到以下代码。

<div class="items container">
    <div class="item">
        <div class="property propertyA">Some stuff</div>
    </div>
</div>

我们典型的 jQuery 选择器看起来非常类似于:

$("div.items.container > div.item > div.property.propertyA").map(doStuff());

问题是我们非常频繁地调用类选择器,以至于执行 JS 的大部分时间都花在运行 jQuery 的内部 curCSS 方法上。

虽然我们可能只有一个项目容器,但它可能包含数百个项目,每个项目都有数十个属性。实际上,在任何给定时间,页面上始终存在大约 5K+ html 标签。

我们在客户端捆绑越来越多的逻辑以适应新功能。我觉得这种情况不会持续太久,因为某些浏览器在某些情况下已经显示出致命的“您的页面没有响应”消息。我们正在考虑很快重构整个客户端。

我正在考虑在新的 xml 命名空间中实现一些新的 XHTML 标记,这样它可能看起来像:

<items>
    <item>
        <propertyA>Some Stuff</div>
    </item>
</items>

然后我将能够选择以下方式:

$("items > item > propertyA")

或者如果我错了直接

$("propertyA")

纠正我,但通过摆脱我的缓慢 CSS 选择器支持一些“getElementsByTagName”可以节省我很多周期,同时使我的 CSS 更可爱,我的整体代码更整洁。这样的解决方案有效吗?

BTW:这里我们假设 IE 不存在并且 firefox 3+ || 运行Chrome 3+ 是我们的目标受众。

I develop for a web app startup and I come across the following code quite often.

<div class="items container">
    <div class="item">
        <div class="property propertyA">Some stuff</div>
    </div>
</div>

Our typical jQuery selector looks pretty much similar to:

$("div.items.container > div.item > div.property.propertyA").map(doStuff());

The thing is that we call class selector VERY often up to the point where most of the time spent executing JS is spent running jQuery's internal curCSS method.

While we might only have one items container it might contains several hundreds of items each with dozens of properties. Actually there is always around 5K+ html tags present on the page at any given time.

We are bundling more and more logic in the client side to accomodate new features. I feel this won't be sustainable for long as some browsers are already showing the deadly "your page is unresponsive" message under some conditions. We are thinking about refactoring the entire client side pretty soon.

I was thinking about implementing some new XHTML tags in a new xml namespace so it could look like:

<items>
    <item>
        <propertyA>Some Stuff</div>
    </item>
</items>

I would then be able to select the following way:

$("items > item > propertyA")

or directly

$("propertyA")

Correct me if I'm wrong but by getting rid of my slow CSS selectors in favor of some "getElementsByTagName" could save me a lot of cycle here while making my css cuter and my overall code neater. Would such a solution work?

BTW: here we run with the assumption that IE does not exists and firefox 3+ || Chrome 3+ is our target audience.

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

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

发布评论

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

评论(2

只有一腔孤勇 2024-09-15 20:19:58

我不知道你的建议是否真的有必要或者会大大提高速度。但是,如果您知道“property”或“propertyA”类始终位于“item”类内,那么仅使用 .property.propertyA< 会更有效/code> 作为选择器。

如果您的 .items.container 元素对于页面来说是唯一的,请考虑在其上使用 ID。然后 jQuery 将首先选择 ID,然后只在其中查找项目。并且不要将标记名称添加到选择查询中,除非您想要排除其他元素(例如

...

)。

如果您重复调用选择器,则应该缓存结果以防止重复搜索(尽管我相信 jQuery 1.4 为您做到了这一点)。像这样的东西:

var $properties = $(".items.container > .item > .property.propertyA");
$properties.map(doStuff());
//later on...
$properties.map(doStuff());

I don't know if what you're suggesting is really necessary or will improve speed that much. However, if you know that the "property" or "propertyA" classes will always be inside the "item" class, then it would be more efficient to just use .property or .propertyA as the selector.

If your .items.container element is unique to the page, consider using an ID on it instead. Then jQuery will first select the ID and only look inside that for the items. And don't add the tag names to the selection query unless you have other elements you want to exclude (like <p class="propertyA">...</p>).

If you are repeatedly calling the selector, you should cache the result to prevent repeating the search (although jQuery 1.4 does this for you I believe). Something like:

var $properties = $(".items.container > .item > .property.propertyA");
$properties.map(doStuff());
//later on...
$properties.map(doStuff());
長街聽風 2024-09-15 20:19:58

只要您制作自定义 xlmns,这就是一个好主意,任何可以缩短开发时间的主意都是好主意。另外,这是一个封闭的环境吗?或者您只是关闭 IE 支持?

As long as you make the custom xlmns it is a great idea, anything that trims down your development time is a great idea. Also, is this for a closed enviroment? or are you just turning off IE Support?

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