替代嵌套锚标签

Wil*_*ill 16 html css

我正在尝试构建一个表,其中每个单元格的内容都包含一个A设置为完整高度和宽度的标记,以便整个单元格可以单击.

但是有些单元格需要在其内容中包含其他链接.

立即跳出的解决方案是嵌套A标签,如下所示:

<td>
   <a href="#" class="cell" >
      Officers include:
      President, Vice president, Secretary, Treasurer,
      <a href="#">7 others</a>
   </a>
</td>
Run Code Online (Sandbox Code Playgroud)

但嵌套A标签是非法的.是否有任何解决方法可以让我达到预期的效果?

ale*_*lex 16

您可以使用CSS或JavaScript.我建议只使用CSS.

CSS

这仅适用于使用CSS的Firefox.基本上我只是让所有子链接(大的除外)都position: relative设置z-index为高于大背景链接.

HTML

<div>
    Hello, <a href="http://example.com/hello" class="normal">Bob</a>
    <a href="http://example.com" class="big"></a>
</div>
Run Code Online (Sandbox Code Playgroud)

CSS

div {
    position: relative;
}

.big {
  display: block;
  width: 100%;
  height: 100%;
  position: absolute;
  top: 0;
  left: 0;   
  z-index: 0;
}

.normal {
  position: relative;
  z-index: 1;  
}
Run Code Online (Sandbox Code Playgroud)

JavaScript的

将单击事件附加到单元格,并将事件附加到所有子链接.确保子链接事件不会冒泡(stopPropagation()).

  • 不幸的是,这将使div内的文本无法选择:( (3认同)