外部jQuery根本不执行 [英] External jQuery simply not executing

查看:78
本文介绍了外部jQuery根本不执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将下面的代码放在运行页上的<script></script>标记内时,它们可以完全正常工作.自那以来,出于组织目的,我将代码移至外部.js文件中,这导致代码停止工作-当应触发某些事件时,什么也没有发生.我确保脚本已包含在给定的页面中,此外,我还通过"view-source"(当我单击脚本的路径时,该脚本已加载到新窗口中)确保该链接有效.

The code below was fully working when it was placed inside <script></script> tags on the running page. I since moved the code to an outside .js file for organizational purposes which caused the code to stop working - nothing happens when certain events should be fired. I ensured that the script was being included on the given page, and furthermore, I ensured that the link was valid through "view-source" (when I clicked on the script's path, the script loaded in a new window).

脚本声明:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script src="js/main.js"></script>

那么,这个外部文件中的JS是什么样的?

So, what does my JS look like in this external file?

(function(){

$('#display-reply-box').click(function(){
    $(this).hide();
    $('#submit-reply-wrapper').show();
});

})(jQuery);

出于可读性考虑,我删除了大多数方法,但这就是设置文件的方式.我确保控制台中没有.js错误,但是,我确实收到了有关jQuery和Firelite的以下错误

I removed most of the methods for readability, but that is how the file is setup. I ensured that there were no .js errors in the console, however, I did get the following errors regarding jQuery and Firelite

Failed to load resource: the server responded with a status of 404 (Not Found) http://getfirebug.com/releases/lite/skin/xp/pixel_transparent.gif Failed to load resource: the server responded with a status of 405 (Method Not Allowed) http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js

Failed to load resource: the server responded with a status of 404 (Not Found) http://getfirebug.com/releases/lite/skin/xp/pixel_transparent.gif Failed to load resource: the server responded with a status of 405 (Method Not Allowed) http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js

我认为上述错误与我遇到的问题有关,尽管我没有运气让它们消失.有时错误在那里,而其他时候没有.

I would assume the above errors have something to do with the problem I'm experiencing, although I've had no luck getting them to disappear. At times the errors are there, other times they are not.

推荐答案

似乎您正在尝试在可能未加载到DOM中的元素上运行某些内容.尝试将其包装在DOM Ready事件处理程序中,如下所示:

It seems as though you're attempting to run something on an element that might not have loaded in the DOM. Try wrapping it in a DOM Ready event handler like this:

(function($) {
    $(document).ready(function() {
        $('#display-reply-box').click(function(){
            $(this).hide();
            $('#submit-reply-wrapper').show();
        });
    });
})(jQuery);

这篇关于外部jQuery根本不执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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