简体   繁体   English

如何在移动浏览器(Chrome)中禁用 onclick 文本突出显示?

[英]How to Disable onclick text highlight in mobile browser (Chrome)?

I'm using Boostrap 4 to build a Web.我正在使用 Boostrap 4 来构建一个 Web。 A simple code such as below:一个简单的代码如下:

<div class="col-12 p-2" id="Maincourse">  
  <h1 class="m-0">Main course</h1> 
</div>

I use my Android Chrome Browser(version 80.0.3987.149) click on the text, it will highlighted the text and popup google search.我使用我的 Android Chrome 浏览器(版本 80.0.3987.149)点击文本,它会突出显示文本并弹出谷歌搜索。

在此处输入图像描述

How can i disable it?我该如何禁用它? I Don't want to set user-select: none because I need to let the user highlight the text when long press.我不想设置user-select: none因为我需要让用户在长按时突出显示文本。

.disable-select {
  user-select: none; /* standard */
  -moz-user-select: none; /* firefox specific */
  -webkit-user-select: none; /* Chrome, Opera and Safari*/
  -ms-user-select: none; /* IE, ms-edge */
}

Source1: https://www.w3schools.com/cssref/css3_pr_user-select.asp来源1: https ://www.w3schools.com/cssref/css3_pr_user-select.asp
Source2: https://developer.mozilla.org/en-US/docs/Web/CSS/user-select来源2: https ://developer.mozilla.org/en-US/docs/Web/CSS/user-select

version compatibility版本兼容性

desktop:

Chrome: 54+
Firefox: 69+
Opera: 41+ (15+ with vendor prefix)
Safari: 3+
MS Edge: 12+
IE: 10+

mobile

Chrome for Android: 54+
Android web-view: 54+
Firefox for Android: 4+
Opera for Android: 14+
Safari on IOS: 3+

refer to the sources for more info and version compatibility.有关更多信息和版本兼容性,请参阅来源。

尝试这个:

-webkit-tap-highlight-color: transparent;

By adding role="dialog" into the wrapper solved my problem.通过将role="dialog"添加到包装器中解决了我的问题。 But I have no idea why, anyone can explain?但我不知道为什么,谁能解释一下?

I've been doing the user-select: none fix for this for awhile and found it unsatisfactory, so I started searching around for a proper name for the feature in order to see if there was better fix/write-up for it and I came across this Google Developers post from Paul Kinlan (apparently the feature is called "Touch to Search"):我一直在做user-select: none fix for this 一段时间,发现它不令人满意,所以我开始四处寻找该功能的正确名称,以查看是否有更好的修复/编写它,我偶然发现了 Paul Kinlan 的这篇 Google Developers 帖子(显然该功能被称为“触摸搜索”):

https://developers.google.com/web/updates/2015/10/tap-to-search https://developers.google.com/web/updates/2015/10/tap-to-search

which describes the behavior in detail and the various ways in which you can disable or enable the behavior.其中详细描述了该行为以及您可以禁用或启用该行为的各种方式。

Relevant excerpts pertaining to your question:与您的问题相关的摘录:

Tap triggering is enabled for any plain text that is selectable and non interactive or not focusable.对任何可选择且非交互或不可聚焦的纯文本启用点击触发。 When the page has a click handler that responds to a tap on text, Touch to Search automatically detects the response and ignores it since we know the developer intended to handle the event.当页面具有响应点击文本的点击处理程序时,Touch to Search 会自动检测响应并忽略它,因为我们知道开发人员打算处理该事件。 Using a touch-and-hold gesture to manually select text also triggers the Touch to Search bar.使用触摸并按住手势手动选择文本也会触发触摸搜索栏。 Users can enable or disable the feature using a preference under Chrome's Privacy settings.用户可以使用 Chrome 隐私设置下的首选项启用或禁用该功能。

As the author of a site there are often times when you don't want a tap gesture on certain element to trigger a search.作为网站的作者,您经常不希望在某个元素上点击手势来触发搜索。 To ensure that Chrome does what you intend, make those elements:为确保 Chrome 能按照您的意愿行事,请制作以下元素:

  1. Focusable: add a tabindex=-1 property on the element.可聚焦:在元素上添加 tabindex=-1 属性。
  2. Interactive: Use any of several standard ways to indicate that an element is interactive:交互:使用几种标准方法中的任何一种来指示元素是交互的:
    • Use accessibility markup to indicate the element has a widget role, or widget attributes.使用可访问性标记来指示元素具有小部件角色或小部件属性。 For example, any element with role=button won't trigger.例如,任何带有 role=button 的元素都不会触发。 Adding accessibility markup has the added benefit that your page will be more readable by visually impaired users.添加可访问性标记具有额外的好处,即您的页面将更容易被视障用户阅读。
    • Any JavaScript click handler that calls preventDefault(), or manipulates the DOM or CSS will not trigger Touch-to-Search.任何调用 preventDefault() 或操作 DOM 或 CSS 的 JavaScript 点击处理程序都不会触发 Touch-to-Search。
  3. Non-selectable: using -webkit-user-select: none;不可选择:使用 -webkit-user-select: none; Non-selectable text will not trigger Touch-to-Search even when using the touch-and-hold gesture.即使使用触摸并按住手势,不可选择的文本也不会触发触摸搜索。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM