简体   繁体   English

jQuery的$(document).ready()是否建议使用

[英]jQuery’s $(document).ready() is it adviceable to use

请我需要一些建议,是否建议使用jQuery的$(document).ready()因为我在使用加载功能加载外部页面时遇到了问题,在此页面上用Google搜索和迷迷糊糊确实想知道我是否可以在不运行jquery脚本的情况下运行$(document).ready()需要您的意见。

Since jQuery manipulates the document, using .ready makes sure your code runs when the document is ready, ie the browser has downloaded the HTML 由于jQuery处理了文档,因此使用.ready可以确保您的代码在文档准备好后即可运行,即浏览器已下载HTML

The load will trigger later, when images are loaded, etc. load图像等时, load将稍后触发。

Also, place your <script> tag just before the <body> tags ends, this will speed up page loading and contribute to a better user experience. 另外,将<script>标记放置在<body>标记结束之前,这将加快页面加载速度并有助于改善用户体验。

I think the best way to look at this is that no one thing is a fix-all for coding. 我认为最好的观察方法是,没有人能完全解决编码问题。 The ready call is great but not for everything your JS does. ready调用很好,但是不能满足您的JS所做的所有事情。 Also, as others have suggested moving your JS before the body tag can help but not always. 另外,正如其他人建议的那样,在body标签之前移动JS可能会有所帮助,但并非总是如此。

Your question should not be should you use it but rather when to use it. 您的问题不应该是您应该使用它,而是何时使用它。

$document.ready() is not called in every partial postback. 并非在每个部分回发中都调用$ document.ready()。 $document.ready() is called only one time ie during first time of page loading. $ document.ready()仅被调用一次,即在第一次页面加载期间。 if you want to get call back of every partial postback then you have to use pageLoad() method which is the part of scriptmanager generated script means if you want to use pageLoad() method then you need to use asp.net scriptmanager in the page. 如果要获取每个部分回发的回调,则必须使用pageLoad()方法,该方法是scriptmanager生成的脚本的一部分,这意味着如果要使用pageLoad()方法,则需要在页面中使用asp.net scriptmanager 。

JQuery is highly recommended for it cross-browser compatibility, community support, ease of use, and battle-tested. 强烈建议使用JQuery,因为它具有跨浏览器的兼容性,社区支持,易用性和经过测试的能力。

$(document.ready()) is equivalent to: $(document.ready())等效于:

document.addEventListener("DOMContentLoaded", function(event) {
    // code goes here

});

The window.load function or JQuery load is equivalent to window.load函数或JQuery加载等效于

document.addEventListener("load", function(event) {
    // code goes here

});

The DOMContentLoaded fires when all of the HTML DOM has parsed. 解析所有HTML DOM后,将触发DOMContentLoaded。 Load fires when all of the HTML DOM has parsed and images have finished loading 当所有HTML DOM都已解析且图像已完成加载时,将触发加载

To use this method you must ensure you have included the JQuery library and it has loaded beforehand. 要使用此方法,必须确保已包含JQuery库并且它已预先加载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM