html - 我可以在 <li> 周围的 <ul> 中使用 <a> 吗

标签 html validation

我得到了以下代码:

<ul> 
     <a href="./index.php?profile=User1">
        <li>User1 : 16</li>
     </a>
     <a href="./index.php?profile=User2">
        <li>User2 : 4</li>
    </a>
</ul>

这在所有主流浏览器中都工作得很好,但它是不允许的/无效的 HTML,正确的方法应该是这样的:

<ul>
    <li> 
        <a href="./index.php?profile=User1">User1 : 16</a>
    </li>
    <li> 
        <a href="./index.php?profile=User2">User2 : 4</a>
    </li>
</ul>

但是如果我像第二个例子那样做,只有文本而不是整个 <li>像我想要的那样可以点击。
我应该继续使用无效的工作版本还是有更好的解决方案?

最佳答案

使用 CSS 使链接占据整个列表项,例如。 display: block(以及您可能需要的任何其他样式)。

环绕列表项的链接是无效的 HTML。

关于html - 我可以在 <li> 周围的 <ul> 中使用 <a> 吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16211326/

相关文章:

javascript - 使用 jquery 计算 'table' 标签的数量

javascript - 我需要更改 cookie 的 javascript 代码。我正在保存输入文本,但我需要保存 div 文本而不是输入文本

css - VS2012 - CSS\9 中的验证警告

java - 在 Web 应用程序中验证数据的位置(使用 Spring)

用于多种数字大小的 ASP.Net ValidationExpression

jQuery/CSS : showing a div at the bottom of ages causes scroll bar and you can't see the div

php - 我可以使用 PHP 直接上传到 Amazon S3 存储桶而不将其保存在本地系统中吗?

javascript - 为什么 DOM 与源代码中的属性大小写会发生变化?

javascript - 使用meteor-autoform预览数据

javascript - 漂亮的表单验证