【问题标题】:How to increase the clickable area of checkbox for touch screen如何增加触摸屏复选框的可点击区域
【发布时间】:2019-08-26 22:38:24
【问题描述】:

我正在尝试增加标签中复选框的可点击区域。

但是,其中还有 2 个锚标记,当我尝试单击平板电脑上的复选框时,我会被重定向到超链接,因为复选框的可单击区域很小,但锚标记的区域更大。

我需要让它们在平板电脑上都可以点击,但也可以点击复选框。

这是当前的布局:

由于我也不能将 :before 作为 jQuery 上的一个 DOM 元素,所以我也不能修改它。 有什么建议吗?

[type="checkbox"]:not(:checked)+label:before,
[type="checkbox"]:checked+label:before {
    content: '';
    position: absolute;
    left: 0;
    top: 1px;
    width: 16px;
    height: 16px;
    border: 1px solid #000;
    background: transparent;
}
<label class="form-label checkout-label">
 <span class="form-label-forced-span">
 I agree with the <a href="$url('Page-Show', 'cid', 'policy-link')$" 
 target="_blank" aria-label="confidentiality and protection 
 policy">confidentiality and protection policy</a> and <a href="$url('Page- 
 Show', 'cid', 'terms-and-uses')$" target="_blank" aria-label="the terms of use">the 
 terms of use</a>.
 </span>
</label>

【问题讨论】:

  • 请也分享您的html
  • @TVBZ 分享了上面的相关html代码。谢谢。

标签: jquery css checkbox mouseevent


【解决方案1】:

我不明白 peudo 元素在标签前产生正方形的原因...

我建议你使用一些@media 规则来根据屏幕大小更改复选框大小:

@media screen and (max-width: 736px){
  [type="checkbox"]{
    width: 2em;
    height: 2em;
    transform: translateY(0.5em);  /* To move it down a bit... */
  }
}
<input type="checkbox">
<label class="form-label checkout-label">
  <span class="form-label-forced-span">
    I agree with the <a href="$url('Page-Show', 'cid', 'policy-link')$" 
                        target="_blank" aria-label="confidentiality and protection 
      policy">confidentiality and protection policy</a> and <a href="$url('Page- 
      Show', 'cid', 'terms-and-uses')$" target="_blank" aria-label="the terms of use">the 
    terms of use</a>.
  </span>
</label>

在小于 736 像素的屏幕上,这将使复选框大小“加倍”。看看common breakpoints


编辑
从您的代码...如果您使用margin-topmargin-left,而不是topleft 来定位伪元素,它工作得很好。

看这里:

.container{
  width:90%;
  margin: 0 auto;
}

[type="checkbox"]:not(:checked)+label:before,
[type="checkbox"]:checked+label:before {
  content: '';
  position: absolute;
  /*left: 0;
  top: 1px;*/

  margin-left: -2.5em;
  margin-top: -1em;

  width: 3em;
  height: 3em;
  border: 1px dotted red;
  background: transparent;
}
<div class="container">
  <br>
  <br>
  <br>
  <input type="checkbox" id="acceptPolicy">
  <label class="form-label checkout-label" for="acceptPolicy">
    <span class="form-label-forced-span">
      I agree with the <a href="$url('Page-Show', 'cid', 'policy-link')$" 
                          target="_blank" aria-label="confidentiality and protection 
        policy">confidentiality and protection policy</a> and <a href="$url('Page- 
        Show', 'cid', 'terms-and-uses')$" target="_blank" aria-label="the terms of use">the 
      terms of use</a>.
    </span>
  </label>
</div>

我在标签上添加了for= 属性...
我还向其中添加了一个容器,只是为了将标签向右“移动”一点。我更改边框颜色只是为了好玩。

【讨论】:

  • 您好!项目中做了增加复选框的可点击区域。当您单击除锚标记之外的标签时,该复选框也会被选中。问题是,我无法编辑布局,所以它应该保持不变。您还有其他可以增加可点击区域的建议吗?
  • «当您单击标签时复选框也会被选中» 使用for= 属性?复选框是在标签内部还是外部? (因为您发布的 CSS,我之前假设在外面)。
  • 如果从表单元素、链接或具有 tabindex 属性的元素生成,则可以单击伪元素。带有标签和复选框的示例:codepen.io/gc-nomade/pen/WWNNwP 它们可用于增加可点击区域codepen.io/gc-nomade/pen/bJGGeQ
  • @G-Cyr:mmm 好的,我记得 JS /jQuery 无法访问伪元素......所以你是对的。我建议您使用 OP 的 HTML 发布您的答案。 ;)
  • 不,它在标签内,我正在使用标签中的属性。我需要对代码进行一些编辑,因为无法共享所有代码。我正在寻找基于 jQuery 或 javascript 的基于点击事件的解决方案,因为这是我能想到的解决方法。你认为有可能通过重叠某个区域的其他元素来增加可点击区域吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多