javascript 本地 jQuery.js 文件不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12771654/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 17:04:48  来源:igfitidea点击:

Local jQuery.js file not working

javascriptjquery

提问by Sap

I had downloaded jQuery.js file from jQuery.com .I have saved this file in 3 places, including JRE/Lib and desktop (where my HTML file which calls it is), to be sure that the jQuery.js file is found. I reference this js file as :

我已经从 jQuery.com 下载了 jQuery.js 文件。我已将此文件保存在 3 个位置,包括 JRE/Lib 和桌面(我调用它的 HTML 文件所在的位置),以确保找到 jQuery.js 文件。我将此 js 文件引用为:

<head>
        <script type="text/javascript" src="jquery.js"></script>
    <script type="text/javascript">
        $(function(){
            $("#clas").click(function(){
                $(this).hide();
            });
        });
    </script>
</head>

<body>
    <p id="clas"> Hello</p>
    <p>Hi</p>
</body>

When I ran this HTML file on Mozilla browser, I expected 'Hello' to vanish when I clicked on it, but it did not. It remained as solid as ever. But when I used a jQuery CDN:

当我在 Mozilla 浏览器上运行这个 HTML 文件时,我希望当我点击它时“你好”会消失,但它没有。它一如既往地坚固。但是当我使用 jQuery CDN 时:

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

And when I used an online HTML editor called Tryit Editor v1.5, it worked correctly! It seems only the local jQuery.js is not doing its part. The JavaScript works fine, only the $() part doesn't. I'm using jdk1.6. I wonder why this snag has occurred. How to resolve it? Help.

当我使用名为 Tryit Editor v1.5 的在线 HTML 编辑器时,它工作正常!似乎只有本地 jQuery.js 没有发挥作用。JavaScript 工作正常,只有 $() 部分没有。我正在使用 jdk1.6。我想知道为什么会出现这个障碍。如何解决?帮助。

回答by Sap

Thanks! I found the solution to this problem, from a similar question posted in this forum, asked a year ago. Here is the link:

谢谢!我从一年前在本论坛上发布的类似问题中找到了解决此问题的方法。链接在这里:

jQuery code doesn't work if I'm using a local jquery.js file, why?

如果我使用本地 jquery.js 文件,jQuery 代码不起作用,为什么?

The problem seems to have been incompatible encoding of the html and the js files. So I added the charset attribute to script tag of js. And the problem and 'Hello' both vanished at a click!

问题似乎是 html 和 js 文件的编码不兼容。所以我在js的script标签中添加了charset属性。点击一下,问题和“你好”都消失了!

回答by superachu

Your code works for me. Please check the below code, I have just modified the location of the jquery.js file where mine is stored in a different location.

你的代码对我有用。请检查下面的代码,我刚刚修改了 jquery.js 文件的位置,我的文件存储在不同的位置。

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<%--<script type="text/javascript" src="jquery.js"></script>--%>
<script type="text/javascript" src="../Scripts/jQuery/jquery-1.7.2.js"></script>
<script type="text/javascript">
    $(function () {
        $("#clas").click(function () {
            $(this).hide();
        });
    });
</script>
</head>

<body>
<p id="clas">Hello</p>
<p>Hi</p>
</body>

</html>

I assume the location of your js is not correct. Are you using the same path of js where you have this "html" or jsp page? Or you have the js files in a separate folder?

我假设您的 js 的位置不正确。您是否使用与此“html”或 jsp 页面相同的 js 路径?或者你有一个单独的文件夹中的 js 文件?

Additionally, you can try alternate way as below:

此外,您可以尝试以下替代方法:

$("#clas").live("click", function () {
        $(this).hide();
    });

Please let me know if this helps.

请让我知道这可不可以帮你。