简体   繁体   English

HTML中的嵌套文本链接

[英]Nested text links in HTML

In HTML nested links are not permitted. 在HTML中,不允许嵌套链接。 However, for my purpose (text notes which sometimes refer to whole sentences and sometimes to just one single word within already anotated sentences) I need them. 然而,为了我的目的(文本注释有时指的是整个句子,有时只是在已经分配的句子中只有一个单词)我需要它们。 So I have to find a way to solve this problem. 所以我必须找到解决这个问题的方法。

However, all I have now is a basic idea on how it should look and behave. 但是,我现在所拥有的只是它应该如何看待和表现的基本概念。 The following mock up shows two links: one to target A, one to B. The "outer" a link is, the lower is the line under it. 下面的模拟显示了两个链接:一个到目标A,一个到B.“外部”是一个链接,下面是它下面的行。 A is the outer link, thus, its line is lower than that of B. Clicking on the lines of a link should always lead to the target of that link - even if the text above that line is the text of an inner link. A是外部链接,因此,它的行低于B的行。单击链接的行应始终指向该链接的目标 - 即使该行上方的文本是内部链接的文本。

I've tried to show that intended behaviour with hover colors: Blue for A, pink for B. 我试图用悬停颜色显示预期的行为:蓝色代表A,粉红色代表B代表。

嵌套测试链接模型

Any ideas how I could realize this in HTML with the help of CSS (and maybe SVG?). 任何想法如何在CSS的帮助下(也许是SVG?)在HTML中实现这一点。 I'd prefer solutions without scripting, but any suggestions are welcomed. 我更喜欢没有脚本的解决方案,但欢迎任何建议。

You can use <span> s inside links: 您可以在<span>的内部链接中使用:

 a {color: #00f; border-bottom: 1px solid; text-decoration: none;} a span {color: #66f; text-decoration: underline;} 
 <a href="#">Hello, this is link. This is <span>inner link</span> here.</a> 

A small problem or extra work is, you need JavaScript to make them follow the links. 一个小问题或额外的工作是,你需要JavaScript来让他们按照链接。

But as you asked, you can get the UI Effect without any scripting, but the following of link, definitely needs scripting! 但正如您所问,您可以在没有任何脚本的情况下获得UI效果,但以下链接,肯定需要编写脚本!

Expanding on the answer from @connexo, you can wrap them all in a span and use a border-bottom on that. 扩展@connexo的答案,你可以将它们全部包装在一个范围内并在其上使用border-bottom。

 .split-link { border-bottom:1px solid blue; padding-bottom:1px; /* for visual reference only */ } .split-link a { text-decoration: none; } .split-link a.inner-link { text-decoration: underline; text-decoration: red; color:red; } 
 <span class="split-link"> <a href="#outer">Hello, this is a link. It has an </a> <a href="#inner" class="inner-link">inner link</a> <a href="#outer"> here.</a> </span> 

You will want to stay within valid HTML, so your only chance (aside of JS) is splitting the outer link into two links. 您将希望保持有效的HTML,因此您唯一的机会(除了JS)将外部链接分成两个链接。

<a href="#outer">Hello, this is link. This is </a><a href="#inner" class="inner-link">inner link</a><a href="#inner"> here.</a>

.inner-link { color: #66f; text-decoration: underline; }

This will split the blue line in your example into two parts as well, which I assume you do not want. 这会将您示例中的蓝线分成两部分,我假设您不想这样做。 But it's not possible otherwise. 但这是不可能的。

Use JavaScript for best results 使用JavaScript获得最佳效果

I know: 我知道:

I'd prefer solutions without scripting, 我更喜欢没有脚本的解决方案,

but… 但…

any suggestions are welcomed . 欢迎任何建议

You can add an inline onclick handler to a child span : 您可以向子span添加内联onclick处理程序:

<a href="#A">AAAA <span onclick="event.preventDefault(); window.location.assign('#B'); return false;">BBBB</span> AAAA</a>

Or, to be DRY, pass in a reference to the handler instead: 或者,要成为DRY,请传入对处理程序的引用:

<a href="#A">AAAA <span onclick="embedLink('#B');">BBBB</span> AAAA</a>

Definition of handler: 处理程序的定义:

function embedLink(url) {
    event.preventDefault();
    window.location.assign(url);
    return false;
}

Working example: 工作范例:

 a { display: inline-block; text-decoration: none; color: blue; border-bottom: 1px solid blue; padding: 1px; } a .annotation { color: fuchsia; border-bottom: 1px double fuchsia; background-color: white; } a:hover { background-color: lightblue; } a .annotation:hover { background-color: lightpink; } 
 <a href="#A">AAAA <span data-href="#B" class="annotation" onclick="event.preventDefault(); window.location.assign(this.getAttribute('data-href')); return false;">BBBB</span> AAAA</a> 


With JS, you can handle other possibilities as well: 使用JS,您还可以处理其他可能性:

  • Open in new window . 在新窗口中打开 Use: window.open() instead of window.location.assign() . 使用: window.open()而不是window.location.assign()
  • Copy to clipboard . 复制到剪贴板 Add an event listener to the context and copy events on the parent link. context添加事件侦听器并在父链接上copy事件。 In the handler, use document.execCommand('copy') to grab the url from the clicked child span instead; 在处理程序中,使用document.execCommand('copy')从被点击的子跨度中获取URL; perhaps its URL is stored in a data-href attribute. 也许它的URL存储在data-href属性中。
  • Display URL in status bar . 在状态栏中显示URL Add a mouseover event listener. 添加mouseover事件侦听器。 In the handler, set window.status = url . 在处理程序中,设置window.status = url

Thank your all for your answers! 谢谢大家的回答! They all have inspired me! 他们都激励着我!

After some hard thinking and merging your answers together I came to the following solution whose greatest advantage is that the basic functions of all links work without JavaScript. 经过一番艰苦的思考并将你的答案合并在一起后,我得出了以下解决方案,其最大优点是所有链接的基本功能都可以在没有JavaScript的情况下工作。

My main idea is to wrap all links inside a <span> element and, as connexo has suggested, to split up those links which contain links themself. 我的主要想法是将所有链接包装在<span>元素中,并且正如connexo所建议的那样,将这些链接拆分为包含链接的链接。 Thus, the HTML skeleton of my above example looks like this: 因此,上面示例的HTML框架如下所示:

<span>
 <a>AAA</a>
 <span><a>BBB</a></span>
 <a>AAA</a>
</span>

All JavaScript is associated just with the <span> . 所有JavaScript都与<span>相关联。 Onmouseover, it removes the hover-class from all ancestor <span> . 在Onmouseover上,它从所有祖先<span>删除了悬停类。 Onclick, it takes the url of the first link child and redirects there. Onclick,它需要第一个链接子的url并重定向到那里。

The CSS is rather simple. CSS非常简单。 It removes the underline from links and defines just how the span should look like (and behave in case of hover). 它从链接中删除下划线并定义跨度应该如何(并且在悬停的情况下表现)。

Another advantage of this design is that nested nested links are also supported, as you can see in the snippet below. 此设计的另一个优点是还支持嵌套的嵌套链接,您可以在下面的代码段中看到。

 function link_span_click(current_element,current_event) { current_event.preventDefault(); current_event.stopPropagation(); var target_href = current_element.getElementsByTagName('a')[0].href; window.location.assign(target_href); } function link_span_mouse_over(current_element) { while (current_element) { current_element.parentNode.classList.remove('link_span_hover'); current_element = current_element.parentNode; } var target_href = current_element.getElementsByTagName('a')[0].href; window.status = target_href; } function link_span_mouse_out(current_element) { while (current_element) { current_element.parentNode.classList.add('link_span_hover'); current_element = current_element.parentNode; } window.status = ''; } 
 a.nested_link { text-decoration: none; } span.link_span { cursor: pointer; display: inline-block; padding-bottom: 3px; background-color: white; border-bottom: 1px solid blue; } span.link_span_hover:hover { background-color: lightblue; } 
 <div> <span class="link_span link_span_hover" onclick="link_span_click(this, event)" onmouseout="link_span_mouse_out(this)" onmouseover="link_span_mouse_over(this)" > <a href="#a" class="nested_link">AAA</a> <span class="link_span link_span_hover" onclick="link_span_click(this, event)" onmouseout="link_span_mouse_out(this)" onmouseover="link_span_mouse_over(this)"> <a href="#b" class="nested_link">BBB</a> </span> <a href="#a" class="nested_link">AAA</a> </span> </div> <div> <span class="link_span link_span_hover" onclick="link_span_click(this,event)" onmouseout="link_span_mouse_out (this)" onmouseover="link_span_mouse_over (this)"> <a href="#a" class="nested_link">AAA AAA AAA AAA</a> <span class="link_span link_span_hover" onclick="link_span_click(this, event)" onmouseout="link_span_mouse_out(this)" onmouseover="link_span_mouse_over(this)"> <a href="#b" class="nested_link">BBB BBB</a> <span class="link_span link_span_hover" onclick="link_span_click(this, event)" onmouseout="link_span_mouse_out(this)" onmouseover="link_span_mouse_over(this)"> <a href="#c" class="nested_link">CCC</a> </span> <a href="#b" class="nested_link">BBB BBB</a> </span> <a href="#a" class="nested_link">AAA AAA AAA AAA</a> </span> </div> 

Still, there remains one problem: If a rather long textlink gets split up into to lines only the second (or last to be precise) line gets underlined. 仍然存在一个问题:如果一个相当长的文本链接被分成线,只有第二个(或最后一个是精确的)线被加下划线。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM