我见过/听说过关于使用user-select的变体禁用文本选择的所有内容,但这些都不能解决我遇到的问题。在Android上(我假设是在iPhone上),如果你点击并按住文本,它会高亮显示它,并弹出一些小标志来拖动和选择文本。我需要禁用这些(见下图):

我尝试过-webkit-touch-callout,但没有用,甚至尝试了像$('body').on('select',function(e){e.preventDefault();return;});这样的东西也没有用。而像::selection:rgba(0,0,0,0);这样的廉价技巧也不会起作用,因为隐藏这些也无济于事--选择仍然会发生,它会扰乱UI。另外我猜那些旗子还会在那儿。
任何想法都是很棒的。谢谢!
发布于 2012-07-07 03:12:13
参考:
我制作的上面的jsFiddle演示使用了一个插件,让你可以阻止任何文本块在或iOS设备(以及桌面浏览器)中被选中。
它很容易使用,下面是安装jQuery插件后的示例标记。
超文本标记语言示例:
<p class="notSelectable">This text is not selectable</p>
<p> This text is selectable</p>示例jQuery:
$(document).ready(function(){
$('.notSelectable').disableSelection();
});插件代码:
$.fn.extend({
disableSelection: function() {
this.each(function() {
this.onselectstart = function() {
return false;
};
this.unselectable = "on";
$(this).css('-moz-user-select', 'none');
$(this).css('-webkit-user-select', 'none');
});
return this;
}
});每条消息评论: I still need to be able to trigger events (notably, touchstart, touchmove, and touchend) on the elements.
我会简单地使用一个包装器,它是而不是受这个插件影响的,但是它的文本内容是使用这个插件来保护的。
要允许与文本块中的链接进行交互,可以对除链接之外的所有链接使用span tags,并仅为这些span tags添加类名.notSelected,从而保留对锚点链接的选择和交互。
状态更新:此更新的确认您担心,当禁用文本选择时,其他功能可能无法工作。在此更新的jsFiddle中显示的是jQuery单击事件侦听器,它将在单击粗体文本时触发浏览器警报,即使该粗体文本不可选也是如此。
发布于 2012-06-28 12:11:14
-webkit-touch-callout:none;
-webkit-user-select:none;
-khtml-user-select:none;
-moz-user-select:none;
-ms-user-select:none;
user-select:none;
-webkit-tap-highlight-color:rgba(0,0,0,0);这将为每个运行的浏览器禁用它。
发布于 2012-06-28 12:44:18
-webkit-user-select:无;Android 4.1之前不支持(抱歉)。
https://stackoverflow.com/questions/11237936
复制相似问题