通过jQuery找到body标签的最快方法 [英] Fastest way to find the body tag via jQuery

查看:145
本文介绍了通过jQuery找到body标签的最快方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在每篇文档上,我都需要通过jQuery获取body标签,而且我对这个答案比其他任何事情都更加好奇。



假设这是我的网站上每个页面上的body标签:

 < body id =body> 

通过标签名称查询主体标签的速度更快:

  $('body')



<

  $('#body')


解决方案

最快的方式是 $(document.body)

这不涉及任何选择器解析。

$('body')在Firefox和Chrome中的速度要快于 $('#body')



测试


On every document ready I need to get the body tag via jQuery and I'm more curious about the answer to this than anything else.

Let's say this is the body tag on every page on my site:

<body id="body">

Is it faster to query the body tag by just the tag name:

$('body')

or query it by its id:

$('#body')

解决方案

The fastest way would be $(document.body).
This doesn't involve any selector parsing.

$('body') is much faster (in Firefox and Chrome) than $('#body').

Test

这篇关于通过jQuery找到body标签的最快方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆