V
V
Vladimir Rodkin2014-11-06 16:46:07
JavaScript
Vladimir Rodkin, 2014-11-06 16:46:07

What to use instead of href="#"?

Often, controls are rendered using the HTML tag a.
This provides several benefits:

  • if JavaScript is disabled, the following link will work if it is set
  • Navigating controls using the Tab key works
  • Fires an event clickon pressing Enter when the link is in focus

Minuses:
  • Constantly addpreventDefault
  • When you hover over such a link, the browser below shows the address, even if it is not specified

Here is a small test: jsfiddle.net/VovanR/omt6am8z
To solve the issue of navigation and displaying the link address, you can replace the tag spanwith the attribute tabindex="0". But the event clickon pressing Enter will not work. Let's go ahead and replace the tag with button. Now everything is almost perfect. Except that this tag is the hardest to style, especially when using global styles.
What are the best practices?

Answer the question

In order to leave comments, you need to log in

5 answer(s)
V
Vladimir Rodkin, 2014-11-09
@VovanR

I wrote a short article on which you can choose what to use, and what is better to refuse. How not to use a(href="#")

S
SerzN1, 2014-11-06
@SerzN1

You can remove the link attribute and business then. Who prevents to write I button . In general, this is nonsense. There is no need to change the semantics to smart-ass things of incomprehensible content. The link should remain a link, if it's an empty control then just take the span.

T
Tashiro, 2014-11-07
@Tashiro

href="javascript:void(0)" ?

A
Alexander, 2014-11-07
@aspetek

You can do href="" - it behaves like a regular link, when you hover it shows the address of the current page. But you still have to add preventDefault.

N
N. Bekseitov, 2017-01-17
@Mirkom63

Multiple files upload with nested resource using P...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question