大佬教程收集整理的这篇文章主要介绍了jQuery选择器性能,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
$('div@L_903_2@meclass').hover(function() { $(this).children('img').fadeOut(function(){ // do something }); },function() { // do something });
经过一些测试,(通过选择器循环1000次,取9次测试的平均值)我使用了3种不同的选择器,得出的结论是速度是这样的:
> $(this).children(‘img’)运行速度最快-avg约400ms;
> $(‘img’,this) – 平均约900ms;和
> $(this).find(‘img’)运行速度最慢 – 平均约1000ms
这违背了两个函数调用慢于一的逻辑.另外,我已经在内部阅读,jQuery将第二种情况转换为第三种情况,那么第三种情况会更慢吗?
有什么想法吗?
$(本).儿童( ‘IMG’):
<h1> <!-- Is this img? Nope! --> <img alt="" src="..." /> <!-- Never checked. --> <span>Bla bla</span> <!-- Never checked. --> </h1> <a href="#"> <!-- Is this img? Nope! --> <img alt="" src="..." /> <!-- Never checked. --> </a> <img alt="" src="..." /> <!-- Is this img? Yeah,return it! -->
$(本).find( ‘IMG’):
<h1> <!-- Is this img? Nope! --> <img alt="" src="..." /> <!-- Is this img? Yeah,return it! --> <span>Bla bla</span> <!-- Is this img? Nope! --> </h1> <a href="#"> <!-- Is this img? Nope! --> <img alt="" src="..." /> <!-- Is this img? Yeah,return it! --> </a> <img alt="" src="..." /> <!-- Is this img? Yeah,return it! -->
如您所见,使用子项时不会检查三个元素,从而提高性能.
以上是大佬教程为你收集整理的jQuery选择器性能全部内容,希望文章能够帮你解决jQuery选择器性能所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。