我正在尝试加快 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”,因为实际上我只是在容器 div 中的输入文本框中查找“.numeric”类。这是在上面缓存的,以执行change()和bind(),但现在我想在这些上做更多的事情,除了不同类的不同事情。有什么想法如何以最佳的缓存性能实现这一点吗?
最佳答案
由于您拥有一组元素,因此您需要过滤该组元素,而不是在该组中查找,这就是您的代码所做的。
尝试allInputText.filter(".numeric")
关于javascript - 基于父级和类型缓存复杂的jquery选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1836535/