简体   繁体   中英

Why Can't I Make This JS Script Work

I have this script:

$("#test").click(function() {
    alert('hue');
});

And I have this element on my html:

<li>
   <a id="test" href="#contato">Contato</a>
</li>

And I have already imported my scrolling script and jQuery.js script.

<script src="js/scrolling.js"></script>
<script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>

But when I click my element, there's no alert()! What am I doing wrong? How can I fix this?

I'm assuming you mean js/scrolling.js contains

$("#test").click(function() {
    alert('hue');
});

In that case, you will need to include scrolling.js after jQuery. And if those script tags are in the <head> of your html, you will need to wrap in a DOM-ready function, as you can't bind to an element before it exists on the page.

$(document).ready(function() {
    $("#test").click(function() {
        alert('hue');
    });
});

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