admin管理员组

文章数量:1401438

I have this inline JS call to a function inside an anchor tag and when click it fires the JS function however I don't want the page to jump to the top when I click it.

<a href="#" title="link" onclick="javascript:runMe()">Click Here</a>

And I have this inside the function:

function runMe() {
// do something
return false;
}

I thought the return false would be the correct way to prevent it to jump to the top but it doesn't work?

I have this inline JS call to a function inside an anchor tag and when click it fires the JS function however I don't want the page to jump to the top when I click it.

<a href="#" title="link" onclick="javascript:runMe()">Click Here</a>

And I have this inside the function:

function runMe() {
// do something
return false;
}

I thought the return false would be the correct way to prevent it to jump to the top but it doesn't work?

Share Improve this question asked Aug 27, 2011 at 2:17 user381800user381800
Add a ment  | 

1 Answer 1

Reset to default 8

You're not returning the return of the function. :P Basically you are only returning false to the runMe function call but not the element's onclick function call.

do

<a href="#" title="link" onclick="runMe(); return false;">Click Here</a>

or

<a href="#" title="link" onclick="return runMe();">Click Here</a>

本文标签: javascriptPrevent page from jumping to the top onclick JSStack Overflow