【问题标题】:How to disable click when highlighting text突出显示文本时如何禁用单击
【发布时间】:2019-06-30 00:02:13
【问题描述】:

我有一个用例,我有一个带有可点击行元素的表格。每行都有一些我想让用户复制的文本。当我试图突出显示文本时,我想阻止 click 事件。

document.querySelector('tr').onclick = function () {
  window.alert(this.innerHTML);
}
table {
 border: 1px solid;
 margin-bottom: 10px;
}

td {
  padding: 10px;
}
<table>
  <tr>
   <td> This row is clickable </td>
  </tr>
</table>

<div>Try to highlight the text above by moving your cursor. I want to prevent click when you are trying to highlight the text which means you should not get the alert dialog.</div>

【问题讨论】:

  • 请在此处发布您的相关代码
  • 您可以在您的点击事件处理程序中检查用户has selected any text。或者,如果用户选择了任何文本,您可以附加另一个事件侦听器并调用 stopImmediatePropagation。
  • 我相信我的问题现在很清楚了。如果您仍需要更多解释,请在再次投反对票之前告诉我。

标签: javascript events copy-paste highlight


【解决方案1】:

我认为这不是真的那样工作..它主要用于禁用收音机、复选框和其他...这里有一些代码

代码 1:

 <!DOCTYPE html>
<html>
<body>

Try to check this box: <input type="checkbox" id="myCheckbox">

<p>Toggling a checkbox is the default action of clicking on a checkbox. The 
preventDefault() method prevents this from happening.</p>

<script>
document.getElementById("myCheckbox").addEventListener("click", 
function(event){
  event.preventDefault()
});
 </script>

</body>
</html>

代码 2:

      <!DOCTYPE html>
      <html>
      <body>

      <a id="myAnchor" href="https://w3schools.com/">Go to W3Schools.com</a>

      <p>The preventDefault() method will prevent the link above from following the URL.</p>

      <script>
      document.getElementById("myAnchor").addEventListener("click", function(event){
        event.preventDefault()
      });
      </script>

      </body>
      </html>

【讨论】:

  • 这与 OP 的问题有什么关系?
  • 这不是我真正想要的。我现在更新了我的问题。
猜你喜欢
  • 2019-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-05
  • 2015-03-14
  • 2012-03-28
  • 1970-01-01
  • 2010-10-24
相关资源
最近更新 更多