简体   繁体   中英

Return false not working sometimes

I have a page with photo gallery http://dev.dolina-imeniy.ru/fotogalereya/kp_usadba_tishnevo/ I use this to bind click event and return it false

$('a.link_photo').click(function(e) {
var new_img = $(this).attr('href');
var photo_title = $(this).attr('title');
var photo_info = $('.photo_info', this).html();
$('#photo_view img').attr({
    src : new_img
});
$('#photo_title').html(photo_title);
$('#photo_info').html(photo_info);
return false;
    });

But on some images it not work? Why it appears?

Try click on 10 image (ut-1-foto.jpg) in my example to see it.

For some reason you code is breaking, so it does not reach to return false.

You can use e.preventDefault(); to stop the default action

e.preventDefault();

trying calling e.preventDefault()

For more info look here: http://api.jquery.com/event.preventDefault/

The reason for this is that the function only binds to the elements that are already in existent when it is called. Every link created after the the document has loaded will not be bound to this function. To listen for the creation of these elements and to then bind the function to them, you could use the jQuery plugin liveQuery . I hope that helps.

I don't think return false; or event.preventDefault() has anything to do with it. I'm guessing it has to do with how your carousel works. It's no coincidence that your code breaks once the images start repeating - the click event is probably no longer bound. If the element is just being moved, the events should still be set, but if it's being cloned or copied the events might not be.

edit: I can confirm by debugging that your script isn't even called on the 'broken' links.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM