Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
173 views
in Technique[技术] by (71.8m points)

javascript - Script does not work when on <head>

I proved script. its works, but outside of . I am not good on script. maybe its a simple problem.

<head>
<script src="http://code.jquery.com/jquery-1.5.js" type="text/javascript"></script>
<script src="http://0rochymugen.ucoz.com/scriptbestsite.js" type="text/javascript"></script>        
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>

basicly, script just show a

when a mouse its over to image.

$('#img1').mouseover(function () {
$('#p1').show("slow");
});

$("#p1").hover(function () {
$("#p1").hide("slow");
});

when I put script on head. simply, doesnt work.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In some cases, if you are trying to operate on items that are on the page, if you javascript loads and executes before the rest of the page has finished loading, you will get errors and/or your code will not appear to work.

This is one reason it is recommended to put links to javascript files at the bottom of the page.

Another good practice is to only run your when the document has finished loading, in jQuery is is normally done using the following syntax:

$(document).ready(function(){
   // Your javascript
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...