javascript 防止 iOS Webkit 长按链接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9145084/
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
Prevent iOS Webkit Long Press on Link
提问by Tristan Seifert
In an iOS app I'm developing, I show an UIWebView
to allow the user to upgrade their account. The page contains links to show the user other informations, but you hold your finger on the link, a menu like this will pop up:
在我正在开发的 iOS 应用程序中,我显示了UIWebView
允许用户升级他们的帐户。该页面包含向用户显示其他信息的链接,但是您将手指放在链接上,会弹出这样的菜单:
As you can see, it reveals the URL of the page and the action that will be performed, which I do not want the user to see, since I don't want them to be able to copy anything as I disabled the "Copy/Cut/Paste" menu already. How would I go about disabling this menu as well?
如您所见,它显示了页面的 URL 和将要执行的操作,我不希望用户看到这些内容,因为我不希望他们能够复制任何内容,因为我禁用了“复制/剪切/粘贴”菜单。我将如何禁用此菜单?
Thanks for any pointers.
感谢您的指点。
回答by rob mayoff
I think you can disable that popup by setting the CSS -webkit-touch-callout
propertyto none
on the link. You would do this by editing the HTML or CSS file you're loading, not using Objective-C.
我认为您可以通过在链接上设置CSS-webkit-touch-callout
属性来禁用该弹出窗口none
。您可以通过编辑正在加载的 HTML 或 CSS 文件来完成此操作,而不是使用 Objective-C。
回答by Cezar
What you want to do, as Robalready stated, is to disable the default contextual menu of UIWebView
. You can achieve it by adding the following line to webViewDidFinishLoad:
正如Rob已经说过的,您想要做的是禁用UIWebView
. 您可以通过将以下行添加到webViewDidFinishLoad:
[webView stringByEvaluatingJavaScriptFromString:@"document.body.style.webkitTouchCallout='none';"];
回答by B-Money
If you want to both disable the popup ANDuser selection of text on your ENTIRE HTML PAGE, use this on the BODY of your web page or global css.
如果您想在整个 HTML 页面上禁用弹出窗口和用户选择的文本,请在您的网页或全局 css 的 BODY 上使用它。
body {
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
回答by Aaron Gillion
No need to tell the browser you have a link and then force it not to behave like one!
无需告诉浏览器你有一个链接,然后强迫它不表现得像一个!
Just remove the href="void(0)"
completely!
只需href="void(0)"
完全删除!
Your ontouchend
or onclick
attributes will still work.
您的ontouchend
或onclick
属性仍然有效。
If you want the normal blue/underlined look again, you can add this css:
如果你想要正常的蓝色/带下划线的外观,你可以添加这个 css:
a .originalLink { color: blue; text-decoration: underline; cursor: pointer; }
This saves a lot of CPU cycles, especially when there are hundreds of links in the page while the user is smooth scrolling.
这节省了大量的 CPU 周期,尤其是当用户平滑滚动时页面中有数百个链接时。
You can also extend this to regular URLs by using this:
您还可以使用以下方法将其扩展到常规 URL:
<a class="originalLink" onclick="location.href='http://mylink';">Real URL Link</a>
Of course your SEO is out the window, but for a specificmobile web app situation this can be crucial.
当然,您的 SEO 不在考虑范围内,但对于特定的移动网络应用情况,这可能至关重要。
Happy coding!
快乐编码!