我必须在
更新:此答案是在修改之前给出的,现在已经无效了,但我会保留它,以供有用的人使用。
将 a
锚点的 pointer-events: none
。
label[for="my-id"] {
cursor: pointer;
}
label[for="my-id"] a {
pointer-events: none;
}
<input type="checkbox" id="my-id">
<label for="my-id"><a href="#">Some text</a></label>
label[for="my-id"] {
cursor: pointer;
position: relative;
}
label[for="my-id"]::after {
content: ' ';
position: absolute;
left: 0; top: 0;
right: 0; bottom: 0;
}
<input type="checkbox" id="my-id">
<label for="my-id"><a href="#">Some text</a></label>
<a>
标签进行样式设置,而是在标签上使用类。
.my-link-style {
/* To match the cursor style for an <a> */
cursor: pointer;
/* Add more styles here */
}
<input type="checkbox" id="my-id" />
<label for="my-id" class="my-link-style">Some text</label>
<input type="checkbox" id="my-id">
<a href="javascript:void(0)"><label for="my-id">Some text</label></a>
<input type="checkbox" id="my-id">
<label for="my-id"><a href="javascript:void(0)" target="my-id">Some text</a></label>
<script>
document.querySelector("label[for=my-id] > a")
.onclick = function(e) {
var el = document.getElementById(
this.parentElement.htmlFor
);
el.checked = !el.checked;
}
</script>
你需要反转,在a标签中包含标签:
<input type="checkbox" id="my-id" >
<a href="javascript:void(0)"><label for="my-id">Some text</label></a>
<label>
标签内使用<a>
标签。 - John Bupita
元素中有href
属性的原因是什么?如果将其删除,点击文本将切换复选框。 - GOTO 0<a>
标签。如果您希望它看起来像一个链接,请将相同的样式应用于标签或标签内的<span>
。不要为了外观而篡改链接。 - Soviuta
元素上的href
属性不是必需的。规范的先前版本包含了一种模棱两可的表述,但在当前的草案中已经澄清。 - GOTO 0