【问题标题】:jquery stops working when using document ready statement使用文档就绪语句时 jquery 停止工作
【发布时间】:2025-11-26 16:15:02
【问题描述】:

所以我的代码一直运行良好。但是,一旦我放置一个 on document ready 语句,整个脚本就会停止工作。我尝试在文档就绪功能中放置警报,并且它正确通过。

我真的不确定问题是什么,以及我将如何进行调试。任何建议将不胜感激

JS code

$(document).ready(function() {
    $("#sample").effect("transfer", {
        to: $("#{{ $stimuli['sample'][0] }}")
    }, 7000, function() {

      $('#{{$stimuli['sample'][0]}}')
        .append('<a href="response/{{$stimuli['sample'][0]}}"><div class="ui-effects-transfer"></div></a>');

      $('#{{$stimuli['sample'][0]}}').css("border", "10px solid green");
    });

});

一些控制台消息:

    'webkitIndexedDB' is deprecated. Please use 'indexedDB' instead.
firebug-lite.js:15683 'Performance.onwebkitresourcetimingbufferfull' is deprecated. Please use 'Performance.onresourcetimingbufferfull' instead.
http://getfirebug.com/releases/lite/skin/xp/pixel_transparent.gif Failed to load resource: the server responded with a status of 404 (Not Found)
http://localhost:8888/pp/public/public/assets/css/media-queries.css Failed to load resource: the server responded with a status of 404 (Not Found)

新更新遗漏了一些错误。

Failed to load resource: the server responded with a status of 404 (Not Found)
http://localhost:8888/pp/public/task/matching/js/init.js Failed to load resource: the server responded with a status of 404 (Not Found)
http://localhost:8888/pp/public/public/assets/css/media-queries.css Failed to load resource: the server responded with a status of 404 (Not Found)
http://localhost:8888/pp/public/task/matching/js/modernizr.js Failed to load resource: the server responded with a status of 404 (Not Found)
http://localhost:8888/pp/public/task/matching/js/jquery-migrate-1.2.1.min.js Failed to load resource: the server responded with a status of 404 (Not Found)
http://localhost:8888/pp/public/task/matching/js/jquery.flexslider.js Failed to load resource: the server responded with a status of 404 (Not Found)
active:94 Uncaught TypeError: $(...).effect is not a function
firebug-lite.js:11883 Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check http://xhr.spec.whatwg.org/.
chrome-extension://bmagokdooijbeehmkpknfglimnifench/firebug-lite.js Failed to load resource: the server responded with a status of 404 (Not Found)fetchResource @ firebug-lite.js:11884
firebug-lite.js:30396 'window.webkitStorageInfo' is deprecated. Please use 'navigator.webkitTemporaryStorage' or 'navigator.webkitPersistentStorage' instead.
firebug-lite.js:30396 'webkitIndexedDB' is deprecated. Please use 'indexedDB' instead.
firebug-lite.js:15683 'Performance.onwebkitresourcetimingbufferfull' is deprecated. Please use 'Performance.onresourcetimingbufferfull' instead.
http://getfirebug.com/releases/lite/skin/xp/pixel_transparent.gif Failed to load resource: the server responded with a status of 404 (Not Found)
http://localhost:8888/pp/public/public/assets/css/media-queries.css Failed to load resource: the server responded with a status of 404 (Not Found)

【问题讨论】:

  • 你有什么错误吗?检查控制台
  • 你是在这个脚本之前还是之后包含jQuery?控制台可能会说类似 $ undefined variable 的内容。
  • 我的 jquery 脚本是在我包含 jquery 文件之后。检查有关控制台错误的主要帖子的更新。但是,萤火虫中没有显示任何内容
  • 从您更新的错误中:“active:94 Uncaught TypeError: $(...).effect is not a function” 那里有问题可以调试。
  • 您是否尝试过在此处设置断点来验证 $("#sample") 是否存在?

标签: javascript jquery


【解决方案1】:

如果您的任何 HTML 是动态创建的,您应该使用 .on 而不是标准的 .ready,我不确定这是否会解决您的问题,因为我不是 100% 发生了什么问题,因为您已经提供的信息非常少。

还要确保您的&lt;script src='jquery.js'&gt;&lt;/script&gt; 位于html 页面的底部。这样可以确保先加载 html,然后再加载 javascript。下面是.on('ready')的示例

$(document).on('ready', function() {
   // Put your code in here
});

编辑:

好的,我想我可能知道您的问题,请确保将您的 JQUERY UI 脚​​本附加到文档 AFTER 您的 JQUERY。例如,它应该看起来像这样:

<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<script src="YOUR_CUSTOM_SCRIPT_.JS"></script> <!-- Put your document.ready function here -->

【讨论】:

  • 他应该在哪里使用.on()?他没有绑定任何事件处理程序。如果您使用$(document).ready(),则脚本的位置无关紧要。
  • 我试过了。它仍然不起作用。我在页面顶部附加了 jquery 和 jquery ui 原始文件。我认为我应该在顶部放置 jquery 文件?如果 jquery 文件位于底部,我应该将它们放在我的 jquery 代码上方吗?
  • @JPFoster 看起来浏览器没有注册.effect() 函数,而且你的很多资源都没有找到!确保您已正确链接它们!因为您收到 404 表示找不到文件
  • 嗯,有趣,检查我的编辑,也许它会有所帮助。 @JPFoster
  • 别担心!祝你无论创作什么都好运。 @JPFoster +1 也许;)