javascript 如何防止触发onclick函数< tr>当我专注于我的输入内的元素< tr>?

jxct1oxe  于 2023-06-20  发布在  Java
关注(0)|答案(4)|浏览(134)
  1. function focusInputFunction(event){
  2. console.log("focus");
  3. event.stopPropagation();
  4. event.stopImmediatePropagation();
  5. }
  6. function clickTr(){
  7. console.log("tr");
  8. }
  1. <table>
  2. <tr onclick="clickTr()">
  3. <td>
  4. <input id="myInput" onfocus="focusInputFunction(event)"type="text">
  5. </td>
  6. </tr>
  7. </table>

我希望当我关注我的文本字段时,<tr>元素上的事件click不会被执行。
我只想在文本字段上执行焦点事件,而不想在<tr>.上触发click事件。
我该怎么做?

vkc1a9a2

vkc1a9a21#

你可以尝试使用一个 flag 变量和 return 从函数clickTr基于value,如下所示:

  1. let isFocusEvent = false;
  2. function focusInputFunction() {
  3. console.log("focus");
  4. isFocusEvent = true;
  5. }
  6. function clickTr() {
  7. if (isFocusEvent) {
  8. isFocusEvent = false;
  9. return;
  10. }
  11. console.log("tr");
  12. }
  1. <table>
  2. <tr onclick="clickTr()">
  3. <td>
  4. <input id="myInput" onfocus="focusInputFunction(event)"type="text">
  5. </td>
  6. </tr>
  7. </table>
展开查看全部
jobtbby3

jobtbby32#

event.stopPropagation();仅停止焦点事件向父级的传播。单击事件仍将被激发。在输入时将其添加到click事件。验证码:

  1. function focusInputFunction(event) {
  2. console.log("focus");
  3. event.stopPropagation();
  4. }
  5. function clickTr() {
  6. console.log("tr");
  7. }
  8. function clickinput(event) {
  9. event.stopPropagation()
  10. }
  1. <table>
  2. <tr onclick="clickTr()">
  3. <td>
  4. <input id="myInput" onclick="clickinput(event)" onfocus="focusInputFunction(event)" type="text">
  5. </td>
  6. </tr>
  7. </table>
展开查看全部
iklwldmw

iklwldmw3#

通过注册<tr><input>的共同祖先元素来委托"click"事件,然后让事件处理程序在<input>:focused时不做任何事情。🞴 of <tr> and <input> and then have the event handler❉ do nothing should the <input> be :focused .
在下面的例子中,我们有一个<table>和两个<tbody>,其中第二个是隐藏的。每当用户单击<table>或它的一个子元素时,第二个<tbody>就会显示/隐藏。如果<input>:focused on,则不会发生前面提到的行为。
参见事件委托。
🞴 在本例中,<table>注册到“click”事件,但<tr>也可以正常工作。
事件处理程序基本上是一个函数,当一个注册的事件被触发时调用。

示例中有详细注解

  1. // Reference the common ancestor of <tr> and <input>
  2. const ancestorNode = document.querySelector("table");
  3. // Reference the <input>
  4. const focalPoint = document.querySelector("input");
  5. // Register the "click" event to <table>
  6. ancestorNode.onclick = delegateClick;
  7. // "click" event handler passes the (event) Object by default
  8. function delegateClick(event) {
  9. // Reference the element user is focused on
  10. const focused = document.activeElement;
  11. // If the user IS NOT focused on <input>...
  12. if (focused !== focalPoint) {
  13. //...add/remove ".show" from <tbody class="hidden">
  14. document.querySelector(".hidden").classList.toggle("show");
  15. }
  16. }
  17. // A simple event hadler to show <input> is focused on
  18. focalPoint.onfocus = e => console.log("Active element is <input>");
  1. :root {
  2. font: 2ch/1.15 "Segoe UI"
  3. }
  4. table {
  5. table-layout: fixed;
  6. border-collapse: collapse;
  7. }
  8. tbody {
  9. float: left;
  10. }
  11. td {
  12. width: 5rem;
  13. height: 5rem;
  14. padding: 0.25rem;
  15. border: 1px solid black;
  16. }
  17. input {
  18. width: 4.5rem;
  19. font: inherit;
  20. }
  21. .hidden {
  22. width: 0;
  23. visibility: collapse;
  24. transition: width 1s;
  25. }
  26. .show {
  27. width: 10rem;
  28. visibility: visible;
  29. }
  30. .as-console-row::after { width: 0; font-size: 0; }
  31. .as-console-row-code { width: 100%; word-break: break-word; }
  32. .as-console-wrapper { min-height: 100%; max-width: 50%; margin-left: 50%; }
  1. <table>
  2. <tbody>
  3. <tr>
  4. <td>&nbsp;</td>
  5. <td>&nbsp;</td>
  6. </tr>
  7. <tr>
  8. <td>&nbsp;</td>
  9. <td><input placeholder="Enter text"></td>
  10. </tr>
  11. <tr>
  12. <td>&nbsp;</td>
  13. <td>&nbsp;</td>
  14. </tr>
  15. </tbody>
  16. <tbody class="hidden">
  17. <tr>
  18. <td>&nbsp;</td>
  19. <td>&nbsp;</td>
  20. </tr>
  21. <tr>
  22. <td>&nbsp;</td>
  23. <td>&nbsp;</td>
  24. </tr>
  25. <tr>
  26. <td>&nbsp;</td>
  27. <td>&nbsp;</td>
  28. </tr>
  29. </tbody>
  30. </table>
展开查看全部
anauzrmj

anauzrmj4#

在处理冒泡事件时,可以使用event.target检查事件的起源。

  1. function focusInputFunction(event) {
  2. console.log("focus");
  3. //you won't need these lines for this, uncomment if you need them for something else
  4. //event.stopPropagation();
  5. //event.stopImmediatePropagation();
  6. }
  7. function clickTr(event) {
  8. if (event.target.id === "myInput") {
  9. return;
  10. }
  11. console.log("tr");
  12. }
  1. <table>
  2. <tr onclick="clickTr(event)">
  3. <td>
  4. <input id="myInput" onfocus="focusInputFunction(event)" type="text">
  5. </td>
  6. <td> other content in tr (to test onclick)</td>
  7. </tr>
  8. </table>
展开查看全部

相关问题