调用$(document).ready(function(){...});从另一个文件 [英] Calling $(document).ready(function() {...}); from another file

查看:41
本文介绍了调用$(document).ready(function(){...});从另一个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所建议的,我正在尝试调用$(document).ready(function(){...});从另一个文件.代码段如下:

As the title suggested, I'm trying to call the $(document).ready(function() {...}); from another file. The code snippet is as below:

源文件:

$(document).ready(function () {
    alert('document.ready function called!');
    // a lot of code
}

在测试文件中:

TestFile.prototype.testDocumentReadyContents = function () {
    // test code here trying to call the document.ready function
}

我还没有成功.我已经尝试了document.ready.apply(),trigger('ready'),覆盖document.ready函数...但是无法调用它.仅供参考,我正在将其作为单元测试的一部分进行调用.

I haven't had any success on it yet. I have tried document.ready.apply(), trigger('ready'), overriding the document.ready function... but just couldn't call it. FYI I'm invoking it as part of my unit test.

谢谢.

推荐答案

好方法

$(document).ready(documentReady);

function documentReady() {
    alert('document.ready function called!');
    // a lot of code
}

TestFile.prototype.testDocumentReadyContents = function () {
    documentReady();
}

Hackish Way

TestFile.prototype.testDocumentReadyContents = function () {
    $.readyList[0]();
}

这篇关于调用$(document).ready(function(){...});从另一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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