javascript - 如何模拟 jquery 就绪函数

标签 javascript jquery document-ready ready

由于某些原因,我们将从我们的旧版应用程序中删除 jquery(请不要问为什么!)

但是,有 1000 多个由设计师设计的模板文件,正在使用 jquery 就绪功能。我们计划制定以下模拟策略。

<head>
<script type="text/javascript">
    // // http://stackoverflow.com/a/9899701
    (function(funcName, baseObj) {
        // The public function name defaults to window.docReady
        // but you can pass in your own object and own function name and those will be used
        // if you want to put them in a different namespace
        funcName = funcName || "docReady";
        baseObj = baseObj || window;
        var readyList = [];
        var readyFired = false;
        var readyEventHandlersInstalled = false;

        // call this when the document is ready
        // this function protects itself against being called more than once
        function ready() {
            if (!readyFired) {
                // this must be set to true before we start calling callbacks
                readyFired = true;
                for (var i = 0; i < readyList.length; i++) {
                    // if a callback here happens to add new ready handlers,
                    // the docReady() function will see that it already fired
                    // and will schedule the callback to run right after
                    // this event loop finishes so all handlers will still execute
                    // in order and no new ones will be added to the readyList
                    // while we are processing the list
                    readyList[i].fn.call(window, readyList[i].ctx);
                }
                // allow any closures held by these functions to free
                readyList = [];
            }
        }

        function readyStateChange() {
            if (document.readyState === "complete") {
                ready();
            }
        }

        // This is the one public interface
        // docReady(fn, context);
        // the context argument is optional - if present, it will be passed
        // as an argument to the callback
        baseObj[funcName] = function(callback, context) {
            // if ready has already fired, then just schedule the callback
            // to fire asynchronously, but right away
            if (readyFired) {
                setTimeout(function() {
                    callback(context);
                }, 1);
                return;
            } else {
                // add the function and context to the list
                readyList.push({
                    fn: callback,
                    ctx: context
                });
            }
            // if document already ready to go, schedule the ready function to run
            if (document.readyState === "complete") {
                setTimeout(ready, 1);
            } else if (!readyEventHandlersInstalled) {
                // otherwise if we don't have event handlers installed, install them
                if (document.addEventListener) {
                    // first choice is DOMContentLoaded event
                    document.addEventListener("DOMContentLoaded", ready, false);
                    // backup is window load event
                    window.addEventListener("load", ready, false);
                } else {
                    // must be IE
                    document.attachEvent("onreadystatechange", readyStateChange);
                    window.attachEvent("onload", ready);
                }
                readyEventHandlersInstalled = true;
            }
        }
    })("docReady", window);

    // Mock jquery.
    var jQuery = function(baseObj) {
        return {
            ready: function(baseObj) {
                docReady(baseObj);
            }
        }
    };
    var $ = jQuery;
</script>

</head>

请注意,我们倾向于使用以下代码片段来模拟 jquery。

// Mock jquery.
var jQuery = function (baseObj) {
    return {
        ready: function (baseObj) {
            docReady(baseObj);
        }
    }
};
var $ = jQuery;

它适用于案例

  • jQuery(document).ready(function() {...});
  • $(document).ready(function() {...});

但是,我们怎样才能让下面的语法也能工作呢?

$(function() {...});

最佳答案

检查传入的参数是否为函数

var jQuery = function(baseObj) {
    if (typeof baseObj === 'function')
        return docReady(baseObj);

代码:

// Mock jquery.
var jQuery = function (baseObj) {
    if (typeof baseObj === 'function')
        return docReady(baseObj);

    return {
        ready: function (baseObj) {
            docReady(baseObj);
        }
    }
};
var $ = jQuery;

关于javascript - 如何模拟 jquery 就绪函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33624881/

相关文章:

javascript - 返回与 JSON 数组中的计数相同的值,Javascript

javascript - 初学者 javascript/jquery - document.write

javascript - Applescript : wait that the page is load on chrome

javascript - 为什么我不能在控制台中循环滚动浏览器窗口?

javascript - 适用于 DIV 的简单 jQuery 幻灯片

javascript - 查找页面上的所有a href,并为每个href动态添加ng-click事件

javascript - jQuery .val() 只返回表单中的第一个值?如何获取所有值?

jquery - 是否可以仅在特定的浏览器宽度下执行函数?

java - 无法访问从 FTL 文件中的 modelmap 发送的对象列表

JQuery - 通过 alt 或标题选择图像