在一个锚标记内包裹 div 是否是有效的代码?


是的,根据 HTML5 规范,我们可以将 div 放置在锚标记中,例如 −

<a href="#" target="_blank">
   <div class="demo"></div>
</a>

另一个示例 −

<a href= www.tutorialspoint.com>
   <div> this now becomes a link </div>
</a>

HTML5 允许将 <a> 标记包装在 <div> 元素中。因此,<div> 将显示在 <anchor> 标记内。

示例

让我们看一个 <anchor> 标记中的 div 示例 −

<!DOCTYPE html>
<html>
<head>
   <style>
      .container {
         position: relative;
         background-color: orange;
         width: 200px;
         height: 200px;
      }
      .container a {
         position: absolute;
         top: 0;
         bottom: 0;
         left: 0;
         right: 0;
         z-index: 999;
      }
   </style>
</head>
<body>
<h1>Demo Heading</h1>
<div class="container">
  <div class="div__one">
    div one
  </div>
  <div class="div__two">
    div two
  </div>
  <a href="https://tutorialspoint.com"></a>
</div>

</body>
</html>

输出

单击橙色 div,链接将打开 −

更新日期:2022 年 12 月 5 日

6K+ 浏览量

开启你的 职业生涯

完成课程并获得认证

开始学习
广告
© . All rights reserved.