gpt4 book ai didi

javascript - 调用 $(document).ready(function() {...});来自另一个文件

转载 作者:行者123 更新时间:2023-12-01 02:26:31 25 4
gpt4 key购买 nike

正如标题所示,我正在尝试调用 $(document).ready(function() {...});来自另一个文件。代码片段如下:

源文件:

$(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 函数...但就是无法调用它。仅供引用,我将调用它作为单元测试的一部分。

谢谢。

最佳答案

好方法

$(document).ready(documentReady);

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

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

黑客方式

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

关于javascript - 调用 $(document).ready(function() {...});来自另一个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1870906/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com