javascript - Jquery mouse events don't fire -


i have strange problem. example code works [here][1] quiet fine, have same code in aptana studio editor , when try in chrome or eclipse browser events don't fire. can't imagine what's problem, because it's same code ...

html

<!doctype html> <html>     <head>         <title>orderscreen</title>         <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>         <script src="js/script.js" type="text/javascript"></script>     </head>     <body>                 <a href="">test</a>     </body> </html> 

jquery

$("a").mouseup(function() {     cleartimeout(presstimer);     // clear timeout     return false; }).mousedown(function() {     // set timeout     presstimer = window.settimeout(function() {         alert("hcbdhaf")     }, 1000);     return false; }).click(function() {     alert("dfsdg"); }); 

if code quoted, problem elements don't exist of when try hook event handlers them. jsfiddle's default settings hide problem you. (look on left, , you'll see code isn't run until load event fires — very, late in page load process.)

to fix it, either:

  1. move script tags end of document, before or after closing </body> tag. time browser runs script, elements exist. recommendation of yui team , google's web engineers like too.

  2. use jquery's ready event.

in conjunction either of those, might @ using event delegation instead of directly hooking events on elements. mouseup , mousedown handlers attached each a element individually. that's lot of hookups. if there's container of a elements in (body or better yet, nearer), might instead hook event on container (since events bubble) , check see if event originated in a element. jquery supports event delegation, doing of hard work you, via delegate (which because it's explicit) , more recently, 1 of half-dozen variations of arguments pass on.


Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -