导航首页 » 技术教程 » jQuery的Read()方法代替原生JS详解
jQuery的Read()方法代替原生JS详解 117 2024-02-13   

在jQuery 3.0的版本前, ready经典用法是用一个匿名函数,像这样:

$(document).ready(function() {
 // Handler for .ready() called.
});

jQuery 3.0 ready() 变化

在jQuery 3.0发布之前,有以下几种方法称之为ready方法:

在document元素上操作: $(document).ready(handler);

在空元素上操作: $().ready(handler);

或者直接(即不在一个具体的元素上)操作: $(handler);

上述所有命名的变种在功能上是等价的。无论是哪个元素,在DOM加载完毕之后其指定的处理程序都将会被调用。换句话说,这里的DOM加载完毕并不表示在文档中的某个具体的元素,比如img元素,加载完毕。相反,这里表示的是整个DOM树加载完毕。

在jQuery 3.0中,除了$(handler) 其他的ready方法都被弃用。

官方声明为此:

这是因为选择器并没有和ready()建立联系,不仅低效而且会导致浏览器引擎对该方法的行为进行不正确的假设。

ready 事件和 load 事件的区别

当DOM加载完毕且元素能够被安全访问时就会触发ready事件。另一方面,load事件却在DOM和所有资源加载后触发。

可以像下面这样使用load事件:

$(window).on("load", function(){
 // Handler when all assets (including images) are loaded
});

这样的话,不仅仅要等到DOM结构能完全访问,而且还需要等到所有的图片资源完全加载完毕(加载时间取决于图片文件大小)才能执行函数。

正常的DOM操作你可能不需要load事件,但是如果你想要在所有的资源被加载完毕之前展示一个旋转的加载器样式,比如,又或者你想要用JS计算一下图片的大小,这可能是一个好的选择。

你可能不需要jQuery.ready()

ready 方法可以确保代码只在所有DOM元素能被安全操纵时才执行。 但这意味着什么呢?这意味着当你要执行的js代码嵌在HTML中某个片段中时,浏览器也要加载完以下元素才能执行。

就像下面这个例子一样:

<!doctype html>
<html>
 <head>
 <meta charset="utf-8">
 <title>.ready() tutorial</title>
 <script src="https://cdn.jsdelivr.net/jquery/latest/jquery.min.js"></script>
 <script>
  $(function(){ // .ready() callback, is only executed when the DOM is fully loaded
  var length = $("p").length;
  // The following will log 1 to the console, as the paragraph exists.
  // This is the evidence that this method is only called when the
  // DOM is fully loaded
  console.log(length);
  });
 </script>
 </head>
 <body>
 <p>I'm the content of this website</p>
 </body>
</html>

如果你要执行的javascript代码放在body末尾,你就可能不需要使用ready()方法,因为浏览器解析到javascript时你可能试图操纵和访问的DOM元素已经被加载完了:

<!doctype html>
<html>
 <head>
 <meta charset="utf-8">
 <title>.ready() tutorial</title>
 </head>
 <body>
 <p>I'm the content of this website</p>
 <script src="https://cdn.jsdelivr.net/jquery/latest/jquery.min.js"></script>
 <script>
  var length = $("p").length;
  // The following will log 1 to the console, as the paragraph exists.
  console.log(length);
 </script>
 </body>
</html>

原生JavaScript ready()替代

对于现代浏览器以及IE9+,你可以通过监听 DOMContentLoaded 事件实现ready()相同的功能:

document.addEventListener("DOMContentLoaded", function(){
 // Handler when the DOM is fully loaded
});

但是,请注意,如果事件已经发射,回调将不会被执行。为了确保回调总是运行,jQuery检查文档reference)的“readyState”属性,如果属性值变为 complete,则立即执行回调函数:

var callback = function(){
 // Handler when the DOM is fully loaded
};

if (
 document.readyState === "complete" ||
 (document.readyState !== "loading" && !document.documentElement.doScroll)
) {
 callback();
} else {
 document.addEventListener("DOMContentLoaded", callback);
}

包括domReady库,已经实现了这个解决方案。

老版本的IE浏览器

对于IE8及以下的浏览器,你能使用onreadystatechange 事件去监听文档的readyState 属性:

document.attachEvent("onreadystatechange", function(){
 // check if the DOM is fully loaded
 if(document.readyState === "complete"){
 // remove the listener, to make sure it isn't fired in future
 document.detachEvent("onreadystatechange", arguments.callee);
 // The actual handler...
 }
});

或者你可以使用Load事件,如jQuery,这样可以在任何浏览器上运行。这也会导致一个时间延迟,因为它会等待所有的资产被加载。

注意,在这个解决方案中你也要检查readyState,如上文所述,这样能确保回调总是能够被执行。

总结

以上就是这篇文章的全部内容了,如果你正在寻找一种原生js替代ready方法,你可以结合DOMContentLoaded事件一起处理。如果你的系统需要兼容IE话,你要确保DOM加载完成。希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流。



!!!站长长期在线接!!!

网站、小程序:定制开发/二次开发/仿制开发等

各种疑难杂症解决/定制接口/定制采集等

站长微信:lxwl520520

站长QQ:1737366103