基于父& amp缓存复杂的jquery选择器类型

时间:2009-12-02 23:09:52

标签: javascript jquery performance

我正在努力加快jQuery的性能。我在这个网站上找到了一些问题/答案,但是我试图更进一步......

以下是HTML的示例,非常简化

<div id="Container">
    <div>
        <div>
            <input type="text" id="something">
        </div>
        <input type="text" id="other">
    </div>
</div>

现在我的jQuery ......

// select all input text boxes inside the div with ID "Container"
var allInputText = $("#Container input:text");

// the inner workings of these have been removed...
allInputText.change().bind();

// Now I have 10+ class checks where I run a function on them...
$(".numeric").numeric();
// ...
$(".etc").etc();

目标是提高10级以上检查的速度。它们都是div“Container”中的相同文本框,只是指定了不同的类。如果我这样做,它可以工作:

var myContainer = document.getElementById("Container");
$(".numeric", myContainer).numeric();

但如果我这样做,它就不起作用了:

$(".numeric", allInputText);

理想情况下,我想使用“allInputText”,因为我实际上只在Container div中的输入文本框中查找“.numeric”类。这是在上面缓存以执行change()和bind(),但现在我想对除了不同类的不同事物之外的那些做更多。任何想法如何以最佳的缓存性能实现这一目标?

1 个答案:

答案 0 :(得分:2)

由于你有一组元素需要过滤集合,而不是代码中的查找,这是你的代码所做的。

尝试allInputText.filter(".numeric")