gpt4 book ai didi

javascript - jquery $(document).one ('ready' , function () {...} 不起作用

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:12:24 27 4
gpt4 key购买 nike

当使用 $(document).ready(function(){...});(版本 1)时,文档就绪函数会无限期地触发。我的猜测是每次 jquery .load 函数完成时都会触发 ready 吗?想确认一下。

所以我尝试使用 jquery 的 $(document).one('ready', function(){...}); 函数(版本 2),但该函数不会在全部。这里有什么问题?

index.html:

<!DOCTYPE html>
<html lang="en">
<head>
<script src="/vendor/jquery/jquery-3.1.0.min.js"></script>
<script src="/js/index.js"></script>
</head>
<body>
<div class="index__header"></div>
<div class="index__content"></div>
</body>
</html>

index.js 版本 1:

var _index = {
loadLogin : function () {
$(".index__content").load("/view/login.html");
}
};

$(document).ready ( function () {
$(".index__header").load("/view/header.html");
_index.loadLogin();
});

index.js 版本 2:

var _index = {
loadLogin : function () {
$(".index__content").load("/view/login.html");
}
};

$(document).one ('ready', function () {
console.log('ready...');
$(".index__header").load("/view/header.html");
_index.loadLogin();
});

最佳答案

$(document).ready() 以一种特殊的方式实现。在文档准备好之前使用它时,它会为 DOMReady 事件建立一个监听器。但是如果在文档准备好后再次调用它,它只是无条件地执行代码,它不会等待事件(因为 DOMReady 事件只发生一次)。

ready 不是真实事件,因此 .one() 无法使用它。

关于javascript - jquery $(document).one ('ready' , function () {...} 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39304035/

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