在移动Safari中点击<标签>

点击<label>不会在移动Safari中自动对焦链接,但如果像这样定义了一个像clickhandler一样的空函数

 document.getElementById("test_label").onclick = function () {}; 

解决了这个问题。

这是完整的源代码。

 <body> <input type="checkbox" id="test" name="test"> <label for="test" id="test_label">This is the label</label> <script> document.getElementById("test_label").onclick = function () {}; </script> </body> 

你知道它为什么有效吗?

它看起来像你在iOS Safari中发现一个错误。 恭喜! 发现和报告错误是令人上瘾的。

您可以在https://bugreport.apple.com/上向Apple报告此错误和其他错误&#x3002; 苹果公司可能不会马上跟进,但是在将来的某个时候,他们应该通知你,这是一个现有的错误的重复,他们不认为它是一个错误,或者(如果你是幸运的),你应该testing它再次在iOS的新版本。

同时,坚持这个解决方法 – 你需要它。

使用FastClick的用户:

使用这个CSS:

 label > * { display: block; pointer-events: none; } 

看到这个问题: https : //github.com/ftlabs/fastclick/issues/60

和这个codepen: http ://codepen.io/visnup/pen/XJNvEq

我们能够通过简单地将这一行代码添加到我们的全局JavaScript文件来解决Etsy的这个问题。

 $('label').click(function() {}); 

我们正在使用xui micro js库,该行简单地将一个空的点击处理程序附加到所有label元素。 出于某种原因,这个奇迹般地解决了移动Safari浏览器的问题。

我知道这不是很好的标记:我只是硬编码像这onclick=""每个标签上的空onclick。 这在一个包装标签上工作:

 <label for="myID" onclick=""> <input type="checkbox" id="myID" name="myNAME"> Check to check </label> 
 <label for="myID" onclick=""> <input type="checkbox" id="myID" name="myNAME"> <span class="name-checkbox"> My name for my checkbox </span> <span class="some-info">extra informations below</span> </label> 

要在标签标签上的任意位置启用iOS,您需要添加到其直接子项(期望input),指针事件:无;

 .name-checkbox, .some-info { pointer-events: none; } 

在iOS 7中,这个问题仍然存在,没有任何解决方法为我工作。

我的解决scheme是将click事件与touchend结合起来:

 var handler = function(e) { /* do stuff */ }; $("ol input[type=checkbox] ~ label") .on('touchend', handler) .on('click', handler); 

在这个JQuery问题中的一些有用的信息: http : //bugs.jquery.com/ticket/5677尤其是技术上说在手机上没有click事件的部分。

添加onclick属性将解决这个问题。

 <label for="test" id="test_label" onclick=""> 

如果已经有一个onclick属性,不应该覆盖它,并在我的testing,它的工作(onclick属性),并点击checkbox。 所以我的解决scheme是:

 <script type="text/javascript"> var labels = document.getElementsByTagName("LABEL"); var labels_l = labels.length; for(var l = 0; l < labels_l; l++){ if(labels[l].hasAttribute("for") && (!labels[l].hasAttribute("onclick"))){ labels[l].onclick = function () {}; } } </script> 

我刚刚解决了类似这样的问题:

 input { position: absolute; width: 120px; // size of my image height: 100px; // size of my image opacity: 0; display: inherit; // Because i'm hidding it on other resolutions } 

一些非常奇怪的行为已经发生在我身上,以上都不能解决这个问题。 如果我在标签元素中放置了文本或图像,则单击该元素不起作用。 但是,当我添加边距和填充标签,单击边距或填充,但不是在文本/图像上工作。 所以我所做的就是将标签100%w 100%h放在我的文字和图片上。

 <div class="wrapper"> <label class="label-overlay" for="file"></label> <img src="something.png"> <p>Upload File</p> <input type="file" name="file"> </div> <style> .wrapper{display:inline-block;} .label{position:absolute;top:0;left:0;width:100%;height:100%;cursor:pointer;} </style>