Jump to content

My First Mistake In Jquery


SandySynaptik

Recommended Posts

Here is source:<!DOCTYPE HTML><html lang="en"><head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>JQuery for Dummies</title><style type="text/css">.centerimage { width: 300px; height: 300px; position: absolute; left: 50%; top: 50%; margin-left: -150px; margin-top: -150px;}</style> </head><body><img src="media/images/image.jpg" height="28" width="28" alt="This is an image" /></body><script type="text/javascript" href="media/js/jquery-1.5.2.min.js" > </script><script type=”text/javascript”>$(document).ready(function(){ alert(jQuery('img').attr('alt')); });</script></html> All the js, css,and images are loading correctly. Why can't I see any prompt box? Another question, if I add class="centerimage" inside the img then browser neglects the height and width

Link to comment
Share on other sites

Don't put the <script> element outside the body. It probably is best even to put it inside the <head>. Having anything outside the head or body sections is incorrect HTML. You might want to relearn HTML before continuing with Javascript. Try removing the height and width attributes from the image, or remove the height and width rules from the CSS, whichever one you're having problems with. I know the CSS precedence rules, but I don't know how it works with HTML attributes.

Link to comment
Share on other sites

@Duotone FoxI have moved both javascripts to head and also removed height and width rules form css file. The page still is as it was, there should be a alert box according to the book. P.S.- Don't be so rude..

Link to comment
Share on other sites

<script type="text/javascript" href="media/js/jquery-1.5.2.min.js" > </script> should be <script type="text/javascript" src="media/js/jquery-1.5.2.min.js" > </script> It seems the quotes used in your code are not proper quotes so the link to jquery, and <script type=”text/javascript”> are not registering as a javascript script tag, (unless it the forum that has rewritten them) select the the quote characters of your code and type the correct shift+2, if you look they seem italic when they should be below <script type="text/javascript">

Link to comment
Share on other sites

@IngolmeThere is no message, error or warning in the error console.@dsonesukWhat are you talking about Shift+2, I have a standard keyboard and there is @ if I press Shift+2 and the files are loading correctly (I can go to that file if I click on its path in view-source mode)

Link to comment
Share on other sites

OK! the code you provided when i pasted in my page the javascript was treated as text because the quotes are " instead of " , on my keyboard shift + 2 produces ", obviously if yours produces @ instead, I would have thought you would have used your brain and guessed that I use a different keyboard layout to you, as not everyone has the same keyboard layout and use the correct keyboard key combination to produce ". The href should not be used when sourcing a javascript file, IT SHOULD BE src. Right the other problem is <script type=”text/javascript”>$(document).ready(function(){alert(jQuery('img').attr('alt'));}); Which is it! you should have an error appear stating that jQuery is not defined, in its default state jquery will use $ to identify as jquery code, to use jQuery you would have to inform it will use this instead with jQuery.noConflict();

Link to comment
Share on other sites

Huh, finally it is ok, validated the page and its done. Validation helps!At last, isn't there any offline html validator? Thanks all for helping an idiot like me, and sorry if your brain's temperature raised in helping me.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...