admin管理员组

文章数量:1315809

I had downloaded jQuery.js file from jQuery .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 :

<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:

<script src=".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.

I had downloaded jQuery.js file from jQuery. .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 :

<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:

<script src="http://ajax.googleapis./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.

Share Improve this question edited Oct 7, 2012 at 18:46 Furqan Safdar 16.7k13 gold badges61 silver badges97 bronze badges asked Oct 7, 2012 at 18:37 SapSap 611 gold badge1 silver badge4 bronze badges 5
  • 1 What do you see in the developer tools? – SLaks Commented Oct 7, 2012 at 18:39
  • 2 Are you sure of the name "jquery.js" (ie not "jQuery.js" if you're on windows, nor "jquery.min.js") ? – Denys Séguret Commented Oct 7, 2012 at 18:39
  • 6 Add alert($); before running your code to make sure it is defined... If it isn't then your path needs adjusting. – scunliffe Commented Oct 7, 2012 at 18:41
  • there is a lot of difference between 'jquery.js' and '/jquery.js' – Prasanth Commented Oct 7, 2012 at 18:55
  • I get the output as: Hello Hi But the Hello does not disappear on clicking, as it should. It is as though the code lines starting with $( have not been executed. The local jquery.js does not work, with/without the '/' before the jquery.js. And I have saved the jquery script as jquery.js in the same folder (desktop) as the html file. – Sap Commented Oct 8, 2012 at 9:21
Add a ment  | 

2 Answers 2

Reset to default 3

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?

The problem seems to have been inpatible 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!

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.

<html xmlns="http://www.w3/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?

Additionally, you can try alternate way as below:

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

Please let me know if this helps.

本文标签: javascriptLocal jQueryjs file not workingStack Overflow