Как отключить клик при выделении текста - PullRequest
0 голосов
/ 27 июня 2019

У меня есть сценарий использования, где у меня есть таблица с интерактивными элементами строки. В каждой строке есть текст, который я хочу разрешить пользователям копировать. Когда я пытаюсь выделить текст, я хочу предотвратить событие 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>

1 Ответ

0 голосов
/ 27 июня 2019

Я думаю, что это на самом деле не работает так ... в основном используется для отключения радио, флажков и других ... вот несколько кодов

Код 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>
...