gpt4 book ai didi

javascript - jQuery .ready() 错误?

转载 作者:行者123 更新时间:2023-11-29 16:16:33 24 4
gpt4 key购买 nike

我正在开发一个 JS 函数来处理 $(document).ready(...) .这些函数在文件“a.js”中。

在函数“example(TAG_ID)”中我调用:

console.log( $('#anything').children().length )

好的。当我在应用程序主 Javascrip 文件中包含时:$(document).ready(example(TAG_ID)) , 返回:0

但是。当我包括 <script>$(document).ready(example(TAG_ID))</script>在id为TAG_ID的元素后返回:7 (应该是这样)

jQuery.ready() 函数的描述是:“指定一个在 DOM 完全加载时执行的函数。”

我觉得是元素加载满后没有运行...为什么?

最佳答案

$(document).ready(example(TAG_ID))

立即评估example(TAG_ID)。正如你引用的文档所说,你想要做的是将 function 传递给 ready() 以便它在 DOM 准备就绪时运行,这将推迟评估:

$(document).ready(function() { example(TAG_ID) })

关于javascript - jQuery .ready() 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14616242/

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