javascript 禁用 iPad Safari 浏览器中的“复制”功能
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14816031/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Disabling the 'Copy' feature in iPad Safari browser
提问by RTores
In Safari for iPad
in iOS6
, I would like to select text and then highlight it (changing the background color) using JavaScript. However, when I do text selection a "Copy" option automatically pops up. How would I go about disabling this "Copy" option? I'm able to get this working on every other browser, except on iPad
Safari.
在 Safari for iPad
in 中iOS6
,我想选择文本,然后使用 JavaScript 突出显示它(更改背景颜色)。但是,当我进行文本选择时,会自动弹出“复制”选项。我将如何禁用此“复制”选项?除了iPad
Safari ,我可以在所有其他浏览器上使用它。
Is this possible? What should I do?
这可能吗?我该怎么办?
回答by Comradsky
If you want to disable the 'Cut / Copy / Paste' invoked by holding down on an element in Safari on the iPhone or iPad use the css:
如果要禁用通过按住 iPhone 或 iPad 上 Safari 中的元素调用的“剪切/复制/粘贴”,请使用 css:
-webkit-user-select: none;
Info from the Disabling ‘hold to copy' on Mobile Safari, posted by Ben Collier
来自移动 Safari 上禁用“保持复制”的信息,由 Ben Collier 发布
the -webkit-tap-highlight-color property accepts any standard CSS color value, but you'll probably want to provide an rgba value in order to control the alpha transparency. Disabling the tap highlight is as simple as setting the alpha value to 0, like so:
-webkit-tap-highlight-color 属性接受任何标准的 CSS 颜色值,但您可能希望提供 rgba 值以控制 alpha 透明度。禁用点击突出显示就像将 alpha 值设置为 0 一样简单,如下所示:
container {
-webkit-tap-highlight-color: rgba(0,0,0,0);
}
Info from Quick Tip: Customizing the Mobile Safari tap highlight colorposted by Ryan Grove
来自快速提示的信息:自定义移动 Safari 点按高亮颜色由 Ryan Grove 发布
So I would use css not javaScript.
所以我会使用 css 而不是 javaScript。
回答by Burak Tokak
Addition to accepted answer;
除了已接受的答案;
Giving the mentioned property to only a carrier div or to body element, holding on some objects (Images and SVG elements etc.) still brings up the tooltip.
仅将上述属性提供给载体 div 或 body 元素,保留某些对象(图像和 SVG 元素等)仍会显示工具提示。
* {
-webkit-user-select: none;
-webkit-tap-highlight-color: rgba(0,0,0,0);
}
Giving the properties to all elements (*) did the trick for me.
将属性赋予所有元素 (*) 对我来说很有效。
Also I'm guessing user-select has a problem/bug with text input, so you might want to exclude it.
另外我猜用户选择在文本输入方面有问题/错误,所以你可能想排除它。
input {
-webkit-user-select: auto;
}