javascript anchor avoid scroll to top on click

前端 未结 5 1611
没有蜡笔的小新
没有蜡笔的小新 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条回答
  •  -上瘾入骨i
    2020-12-08 15:14

    I know that the question is asked for JavaScript, but if anyone would like to do it in jQuery then it is pretty simple.

    You can give your a tag an id and reference it in jQuery to do a certain action.

    All you need to do, - to avoid the scroll to top is to disable the default action of the a tag.

    Code:

    $( "#a-tag-id" ).click(function(e) {
            e.preventDefault();
            alert( "Hello World" );
        });
    

提交回复
热议问题