href link to # question

2019-08-21 10:26发布

问题:

I have a question about the href link, tried googling it but could not find much info on this. I have a href link like this:

<a href='#' onclick='openSerialWindow();return false;'><h:outputText value="#{i18n.regFindSerialNumber}" /></a>

previously the # was replaced by the page.htm that it should link to and that caused an error when user right clicked on the link and chose 'Open in new window/tab'. After replacing the page.htm with # it works fine user can even r/c and open it in new tab/window.

If user simply clicked on the link both ways above worked (the # and page.htm), so I am wondering what is the true meaning of #?

thanks.

回答1:

# is an anchor hash and points to the top of the current page.

You can create anchors in your document like this: <a name='anchor'></a> and then jump to them by adding #anchor to the page's URL. The browser will jump to the position without reloading.

If linking to page.htm produced an error, then page.htm is an incorrect link.

The href attribute is there only as fallback in case there is no Javascript. Javascripts's return false; prevents execution of the link. Linking to # means that if javascript is turned off, nothing will happen by clicking on the link except that the browser will jump to the top of the page.



回答2:

See: http://www.w3.org/TR/html4/struct/links.html

A # indicates a link to a [named] anchor within a page.

An <A> which invokes some javascript generally doesn't work with the "open in new window/tab" command.