javascript anchor avoid scroll to top on click

前端 未结 5 1614
没有蜡笔的小新
没有蜡笔的小新 2020-12-08 14:47

I created a function in javascript that i add to an anchor as such

javascript :

somefunction = function () {alert(\'foo\')}

html :

5条回答
  •  失恋的感觉
    2020-12-08 15:27

    The onclick causes the page to resubmit, therefore scrolling to the top.

    You need the onclick event to return a false in order to avoid the postback.

    This can be done by changing your function to return a false:

    somefunction = function () {alert('foo'); return false;}
    

    Or to change the link and onClick event to do so:

    anchor
    

提交回复
热议问题