- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在寻找 jQuery 的 document.ready() 的原生 JavaScript 解决方案。查看 this thread ,CMS 建议仅使用 jQuery 用于实现其 document.ready() 的 code。我正在查看 bindReady()
但不确定如何将其合并到我的代码中。我目前有这样的东西:
$(document).ready( function() {
console.log('foo');
});
最佳答案
基本上当你需要做的是替换有
的行jQuery.ready();
使用您要调用的函数的名称。如果您想要像 jQuery 的现成注册方法那样工作的东西,请构建一个创建队列的函数。当“就绪”被触发时循环遍历队列。
您要求提供更多信息,所以这里是一个不使用超时的快速而简单的示例。这不是生产就绪,只是一个基本的 POC。
(function () {
var ready = {
_readyQueue: [],
_hasRun: false,
_docReadyCalled : function() {
this._hasRun = true;
this._execute();
},
_execute: function () {
var func;
while (this._readyQueue.length) {
func = this._readyQueue.shift();
func();
}
},
register: function (func) {
this._readyQueue.push(func);
if (this._hasRun) {
this._execute();
}
}
}
window.docReady = ready.register.bind(ready); //use what ever global namespace you want here
function bindReady() {
/* This would be that jQuery code, I am just use window load here so not so much code */
//Not all browser support this, quick and dirty for example
window.addEventListener('load', ready._docReadyCalled.bind(ready), false);
}
bindReady();
})();
/* waiting for DOM to be ready */
docReady(function () { console.log("here"); });
docReady(function () { console.log("there"); });
/* Showing what happens when you call docReady after it is ready */
docReady(function () { console.log("registering ready again"); docReady(function () { console.log("I am here!"); }); });
关于javascript - 原生 JavaScript document.ready - 如何使用 bindReady(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21028335/
根据 jQueryDoc在术语中,.ready() 被称为位于 jQuery.prototype 中的查询选择方法。对于前 $(document).ready() 作为jQueryDoc说:$ 命名空
我见过一些代码,他们只是这样做: $().ready(function() { ... }); 这比执行文档选择器要短,但它是同一件事吗? 最佳答案 略有变化: $(document).rea
各个处理程序的回调函数何时执行? 最佳答案 在主要过程中 当您的应用完成初始化并准备打开浏览器窗口时,将触发app上的“就绪”事件。由于在此之前您无法打开窗口,因此可以使用回调函数来创建Browser
最近发现了head.js图书馆和男孩,我对此感到满意,尽管我仍然对一件事感到有点困惑。 来自 headjs.com: The “DOM ready” event such as $(document)
我在我的代码库中发现代码在另一个 $(document).ready(function() { ... 例如 $(document).ready(function() { // 20
我已经养成了从ready函数开始jquery编码的习惯 $(function(){...}); 并将所有从ready调用的函数放入ready中。 然后我意识到,放入就绪函数中的一些函数可能不需要在那里
在浏览旧代码库时,我发现了以前的软件开发人员正在使用的地方 $(function(a) {}(A || (A = {}))); 即使页面尚未准备好,它仍在执行。在我删除传递的全局变量后它开始工作。代码
我已经看到了两种方式,但哪种更好或者并不重要。 我觉得包装每个语句可能会更干净,但只是想知道如果您有 50 个语句,每个语句都有自己的 document.ready 事件处理程序,是否会有更多回调?
这个问题已经有答案了: Four variations of jQuery ready() -- what's the difference? (4 个回答) 已关闭 4 年前。 我正在编写一些我没有
我们最近遇到了一个错误,jquery document.ready 似乎在 DOM 加载之前触发。事实证明,在调用的第一个项目之后有一些错误的代码 $.ready(function(){}); 这条语
什么应该在 jQuery.ready() 中,什么应该在 jQuery.ready() 之外? 从性能角度来看,我在某处读到,将所有代码包装在 jQuery.ready() 中并不是一种有效的方法。
我实现了一个带有选项列表(工作类别)的页面,单击该页面时应显示数据(工作描述)。我正在使用 BBQ 来处理后台堆栈。 一切正常,除了在用户首次导航到页面时设置初始选择。我的代码被调用到 addClas
解决方案 我有两个 users.js和 users.coffee在我的 Assets 管道中。显然 users.coffee正在阻止 users.js从被加载。确保删除它! 我正在尝试实现 this
我正在研究一个指令,但我不想玩 $document或 $window , 只有 element本身。 之前我有: angular.element($document).ready(function()
我想在移动设备方向改变时使用 $(window).resize 来调用一些函数,我在 $(document).ready 中编写了所有代码,这在我使用 Android 设备时有效,但在使用 iPhon
我有一个使用数据库的 Ionic 应用程序。我想用一个文件的内容填充这个数据库。 这部分我开始工作了。我想创建一个 DB.ready() 事件,很像 $ionicPlatform.ready() 或
我有一个名为“loadTimeTrackersGrid()”的函数,它加载一个弹性网格。设置如下所示: $(document).ready(function () { var edi
我有一个定义数量为 replicas 的部署.如果我的 Pod 准备好/未准备好处理新连接,我使用就绪探针进行通信 - 我的 Pod 在 ready 之间切换/not ready他们一生中的状态。 我
有什么区别: $(document).ready(initialize); 和 $(document).on('ready', initialize); 对我来说,它们似乎以相同的方式工作。 最佳答案
我看到很多项目都在使用 $(document).on('ready', function(e){ //jquery stuff }) 而不是: $( document ).ready(functio
我是一名优秀的程序员,十分优秀!