最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - prevent page jump on .html and .append? - Stack Overflow

programmeradmin4浏览0评论

when i use jquery .html or .append to add elements in the DOM the page jumps to that location.

how can i make it not jumping, cause i want my users to be able to click on "save thread to favourites" and it wont jump to "my saved threads" cause then the user has to navigate down in the list with threads again.

EDIT: i have used this:

 // save thread
    $("a.save_thread").live("click", function(event) {
        event.preventDefault();
        $.post('controllers/ajaxcalls/threads.php', {method: 'save_thread', thread_id: event.target.id}, function(data) {
        }, 'json');
        return false;
    });

but the return false didnt do anything. and actually, although it doesnt use append() or html() it still jumps to page start.

when i use jquery .html or .append to add elements in the DOM the page jumps to that location.

how can i make it not jumping, cause i want my users to be able to click on "save thread to favourites" and it wont jump to "my saved threads" cause then the user has to navigate down in the list with threads again.

EDIT: i have used this:

 // save thread
    $("a.save_thread").live("click", function(event) {
        event.preventDefault();
        $.post('controllers/ajaxcalls/threads.php', {method: 'save_thread', thread_id: event.target.id}, function(data) {
        }, 'json');
        return false;
    });

but the return false didnt do anything. and actually, although it doesnt use append() or html() it still jumps to page start.

Share Improve this question edited Jan 4, 2010 at 9:57 ajsie asked Jan 4, 2010 at 9:41 ajsieajsie 79.9k110 gold badges284 silver badges387 bronze badges 2
  • what browser does this appear on? – kender Commented Jan 4, 2010 at 9:57
  • Any reason you use live instead of click? it seems to me that by the time the click bubbles up to the document, it's too late to prevent he link's default action – K Prime Commented Jan 7, 2010 at 4:07
Add a ment  | 

3 Answers 3

Reset to default 4 +100

return false or preventDefault is the right thing to do to stop the link being followed. Check your JavaScript error console. If there is an exception in the handler function, it won't get to returning false and the link will get followed.

However, you should consider changing your link to a <button>. What you have here is not a link, not a connection to any place in particular; it is an action. A button is a much better way of modelling that, because it doesn't have the behaviours of following a link, or allowing the user to middle-click to open the target of the link in a new tab, or right-click-add-to-bookmarks (and so on). As a consequence, you don't have to worry about preventing the default action to stop the page moving.

If you don't want it to look like a 3D button you can always use CSS to restyle it, eg. border: none; background: white; color: blue; text-decoration: underline;. Whilst you can't totally restyle it in IE which erroneously adds a little extra padding, that typically doesn't matter.

I don't think it's .html() or .append() that do the jump. Rather more, check that the function you are executing returns false:

$('a.pseudolink').click(function () {
    $(this).html('foobar!');
    return false; // !!!
});

This prevents that, clicking on <a class="pseudolink" href="#newcontent">, the link target is activated.

a real kludge to prevent this would be to remove the href attribute from the element before making the post like this:

$("a.save_thread").live("click", function(event) {
  $(this).removeAttr("href");
  $.post('controllers/ajaxcalls/threads.php', {method: 'save_thread', thread_id: event.target.id}, function(data) {}, 'json');        
});

You could also reinstate the href by saving it and adding it back in after you have finished your action.

var href = $(this).attr('href');
//do actions 
$(this).attr('href', href);

I have used this previously with success in my own development, see if it fits your purpose though.

发布评论

评论列表(0)

  1. 暂无评论