开发者

HTML - alternate element for explicit links?

开发者 https://www.devze.com 2023-02-07 10:44 出处:网络
<a> without href? If I want a link\'s text to equal its target, is there no way to declare it without duplicating the target URL in the source?

<a> without href? If I want a link's text to equal its target, is there no way to declare it without duplicating the target URL in the source?

in other words: does a shorter version of this exist?

<a href="http://www.domain.com/page.html">http://www.domain.com/page.html</a>

this does not work:

<a>http://www.domain.com/page.html</a>
开发者_运维百科

is there no element for this purpose?


You are correct, a click-able anchor always needs a hyperlink reference.

As an aside, you could use javascript and short-hand it, but you're adding a dependency to the visitor now.

var links = document.getElementsByTagName('a');
for (var l = 0; l < links.length; l++){
  if (links[l].innerHTML.indexOf('http') == 0 && !links[l].href)
    links[l].href = links[l].innerHTML;
}

Very Primitive, but works.


No. It definitely needs a href attribute.


Not with plain HTML. It is possible with JavaScript, although I don't recommend it since crawlers wouldn't be able to follow the link and that would be just lazy.


is there no tag for this purpose?

no


You can always define it as below and then use some Javascript to do the rest.

<a href="#">http://www.domain.com/page.html</a>

An anchor tag needs a href, it's just how it is. If you don't want to use a href, do:

<span class="mockAnchor">http://www.domain.com/page.html</span>

And have your own CSS for that to make it look like an anchor (and add a click event etc.)

0

精彩评论

暂无评论...
验证码 换一张
取 消