admin管理员组

文章数量:1426644

I'm learning jQuery, and I'm running into a small problem. jQuery code works when I put it directly in my tags in my HTML, but when I import it from a different file, nothing happens. These are my script tags in HTML:

<script type="text/javascript" src="scripts/jquery-1.10.2.js"></script>
<script type="text/javascript" src="scripts/test.js"></script> <!--This is my jQuery code-->

test.js contains the following code:

$(document).ready(function(){
console.log("Hello");
});

When the page loads, the console is empty. However, when I paste the following code as so, in my html document, everything works fine.

<script>

    $(document).ready(function(){
    console.log("Hello");
    });

</script>

I'm learning jQuery, and I'm running into a small problem. jQuery code works when I put it directly in my tags in my HTML, but when I import it from a different file, nothing happens. These are my script tags in HTML:

<script type="text/javascript" src="scripts/jquery-1.10.2.js"></script>
<script type="text/javascript" src="scripts/test.js"></script> <!--This is my jQuery code-->

test.js contains the following code:

$(document).ready(function(){
console.log("Hello");
});

When the page loads, the console is empty. However, when I paste the following code as so, in my html document, everything works fine.

<script>

    $(document).ready(function(){
    console.log("Hello");
    });

</script>
Share Improve this question edited Apr 15, 2015 at 15:50 bytecode77 14.9k31 gold badges117 silver badges149 bronze badges asked Aug 19, 2013 at 6:41 CarpetfizzCarpetfizz 9,22923 gold badges95 silver badges155 bronze badges 6
  • 2 It should work, problem must be somewhere else. Is path an filename correct? – elclanrs Commented Aug 19, 2013 at 6:42
  • Do you see any 404 errors in dev tools? – Ionică Bizău Commented Aug 19, 2013 at 6:43
  • There must be a path declaration problem , otherwise it should be works – Devang Rathod Commented Aug 19, 2013 at 6:44
  • Do you see something like "cannot find file" in errors?. Add jQuery from an online CDN. It might help you to check if this is a path problem. – palerdot Commented Aug 19, 2013 at 6:44
  • No as per this stackoverflow./questions/3859966/… – kbvishnu Commented Aug 19, 2013 at 6:48
 |  Show 1 more ment

4 Answers 4

Reset to default 1

Do you really have a folder named scripts in the folder where your HTML files exist? Otherwise try changing this src="scripts/jquery-1.10.2.js" to probably this: src="/scripts/jquery-1.10.2.js"

You can check by browsing to http://yoururl/scripts/jquery-1.10.2.js, the Jquery code should show up. If it's not there, you have to make sure that you include the right folder/file.

Yes.You will be able to push your scripts in 2 ways:: 1-push your scripts in tag like>>

<script>$(document).ready(function(){alert("Wele Dear");});</script>

2-push your inline scripts with attributes like>>

<button type='button' onclick="$(this).hide()">Hide Me</button>

make sure that the scripts folder contain test.js make sure that the extension is correct ..sometimes I make the same same mistake, instead of test.js i save it as test.js.txt

Try putting

<script type="text/javascript" src="scripts/jquery-1.10.2.js"></script>

in the body rather than the head.

Credits to this answer for giving the idea

本文标签: javascriptDoes jQuery have to be written in the ltscriptgt tags in HTMLStack Overflow