简体   繁体   中英

Local jQuery.js file not working

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 :

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

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

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.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?

Additionally, you can try alternate way as below:

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

Please let me know if this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM