preventing onclick page jumps

前端 未结 4 1914
臣服心动
臣服心动 2020-12-10 05:31

what is the best way to stop my onclick links from causing the page to jump to the top.




        
相关标签:
4条回答
  • 2020-12-10 05:35

    Try this:

    <a href="#" onclick="func(); return false;">link</a>
    

    Notice that the onclick parameter returns false. Returning false cancels the default browser behavior. In the case of an anchor tag, the default browser behavior is to jump to the # anchor (aka the top of the page).

    With this same trick you can also make image un-draggable and ensure that links don't steal the user's focus:

    <img src="coolios.jpg" onmousedown="return false" /> <!-- un-draggable image -->
    <a href="stuff.html" onmousedown="return false">link that doesn't steal focus</a>
    
    0 讨论(0)
  • 2020-12-10 05:45

    Maybe this will do the trick?

    <a href="javascript: return false;" onclick="blahblah">
    
    0 讨论(0)
  • 2020-12-10 05:48

    I always use

    <a href="javascript:void(0)" onclick="blahblah">
    
    0 讨论(0)
  • 2020-12-10 05:55

    Returning false in 'onclick' prevents page jump

    <a href="#" onclick="someFunction(); return false;">blah</a>
    
    0 讨论(0)
提交回复
热议问题