tags:

views:

1725

answers:

4

I want to change image when user click on a thumb image, and prevent it to jump to the top of page.

the first code I wrote:

$('#photos-list img').click(function(e) {
 e.preventDefault();

 var imgObj = new Image();
 var targetObj = $('#main_image img');
 imgObj.onload = function() {
  targetObj.attr('src',this.src);

 }
 imgObj.src = $(this).attr('rel');
});

the preventDefault() works fine. ok, I want to add some effects to the image changing, like fade in, I add some jquery effects in

$('#photos-list img').click(function(e) {
 e.preventDefault();

 var imgObj = new Image();
 var targetObj = $('#main_image img');
 imgObj.onload = function() {
  targetObj.hide();
  targetObj.attr('src',this.src);
  targetObj.fadeIn('normal');

 }
 imgObj.src = $(this).attr('rel');
});

and this time , the preventDefault does not work, even I gave fadeIn() a preventdefault like:

targetObj.fadeIn('normal', function(e1){e1.preventDefault();});

any methods to solve this problem?

+2  A: 

You must be getting an error for the preventDefault to fail (although the js looks ok at first glance). Have you got firebug? Is that the exact source?

redsquare
+1  A: 

try to end funtion with

return false;
Peter Kaleta
That is what prevenDefault does. It serves the same purpose. Must be an error somewhere
redsquare
it;s not exactly the same, see: http://stackoverflow.com/questions/1357118/javascript-event-preventdefault-vs-return-false
Tim
A: 

You need to enable ie support.

Try the following:

$('#photos-list img').click(function(event) {

var ev = event || window.event;

ev.preventDefault();

... the rest of your code

josedasilva
A: 

The callback of the jQuery fadeIn function didn't receive an event parameter.

I asume you have the following structure:

<div id="#photos-list>
  <a href="#"><img src="..." /></a>
</div>

So try this:

$('#photos-list a').click(function(e) {
    e.preventDefault();
});
GiDo