$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>javascript - Why href="#" is High Priority than onclick - Stack Overflow|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Why href="#" is High Priority than onclick - Stack Overflow

matteradmin14PV0评论

I usualy found the following code:

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

but sometime my browser go to the top of the page?

Why href="#" is High Priority than onclick?

I usualy found the following code:

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

but sometime my browser go to the top of the page?

Why href="#" is High Priority than onclick?

Share Improve this question edited Nov 9, 2010 at 8:49 Felix Kling 818k181 gold badges1.1k silver badges1.2k bronze badges asked Nov 9, 2010 at 8:45 freddiefujiwarafreddiefujiwara 59.2k28 gold badges78 silver badges109 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 4

It isn't higher priority. The onclick fires and then the browser follows the link.

If you don't return false (note spelling) or func throws an error (thus not reaching the return statement) the event won't be canceled.

(As fallbacks for if the JS fails or is disabled go, however, a link to the top of the page is really sucky. Progressive enhancement is the way forward.)

If the only reason you want to have a href value is to enable the hand cursor, you can use css style instead:

<a style="cursor:pointer;" onclick="func();return false">click</a>

...and a decade later, here's the answer to the question asked above;

<a href="#" onclick="event.preventDefault(); func()">click</a>

To save confusion, this answer does not address the "return false" aspect. That subject can be researched separately.

PS; Technically there is no hierarchy (AKA "priority") for what happens when an Anchor Tag is clicked. But since HREF is the "default" action / event for an anchor tag, from a certain perspective, HREF does "take priority" over OnClick.

Post a comment

comment list (0)

  1. No comments so far