What is the difference betweeen these lines of code in jquery?
I have a div tag which contains the Content as "I am div tag".When i was trying to Toggle this div when a user click on button. here is the code which i wrote
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>jquery effects (Hide,Animate,SlideDown,SlideUp)</title>
<style type="text/css">
.togg
{
background-color: Aqua;
}
</style>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.1.js">
$(document).ready(function () {
$("p").click(function () {
$(".togg").toggle();
});
开发者_Go百科 });
</script>
</head>
<body>
<div class="togg">
I am a Div Tag
<p>
hi Laxmi ! how r u?</p>
</div>
</body>
</html>
am not getting the output here.But when i write the code like this here am getting the output.please tell me the difference between these lines of code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>jquery effects (Hide,Animate,SlideDown,SlideUp)</title>
<style type="text/css">
.togg
{
background-color: Aqua;
}
</style>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.1.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$("p").click(function () {
$(".togg").toggle();
});
});
</script>
</head>
<body>
<div class="togg">
I am div Tag
<p>
hi Laxmi! how r u?</p>
</div>
</body>
</html>
First piece of code contains an error. You can't include javascript files (by putting their paths in src
attribute of script
tag) and then put the code inside it.
Just use the second code.
In the non-working code, you've got inline javascript inside the <script>
tag that links to jQuery, this won't work.
It's clear: you have to include jquery script whith this
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.1.js"></script>
and then you need another script tag to add your code:
<script type="text/javascript">
$(document).ready(function () {
$("p").click(function () {
$(".togg").toggle();
});
});
</script>
Hence the second one is correct.
the first lines of code.. you don't include the jquery libary correct.
in the second one .. you include it .. afterwards you use it .. that's the correct way.
精彩评论