【问题标题】:Unable to trigger onmouseup event on an <tr> element through JavaScript console on Chrome无法通过 Chrome 上的 JavaScript 控制台在 <tr> 元素上触发 onmouseup 事件
【发布时间】:2018-10-04 17:22:55
【问题描述】:

无法通过 Chrome 上的 Javacript 控制台在 "tr" 元素上触发 onmouseup 事件

元素是这样的(不能分享具体代码抱歉):

< tr class="one" style="height:20px;" onmouseup="click(event,this)"> Text </tr>

在控制台中,我做了以下步骤:

document.getElementsByClassName("one")[0].onmouseup()

我得到错误:

Uncaught TypeError: Cannot read property 'metaKey' of undefined
    at Object.i [as click] (filename:linenumber)
    at HTMLTableRowElement.onmouseup (Workbox?ajax:1)
    at <anonymous>:1:45

在随附的 js 源文件中,在给定行号的函数 i 中, event.metaKey 正在检查中:

    n = b.metaKey || b.ctrlKey

在这一行,错误显示。

当我自己用手(鼠标)单击时,此事件有效。当我使用该命令执行相同操作时,它无法正常运行。由于这个“元密钥”没有正确传递给它。我可以通过 onmouseup() 调用传递它吗?还有其他方法吗?在 mouseEvents 的上下文中,metakey 到底指的是什么? 在其他问题上,据说“Win”键是元键。但是当手动点击时,我不按任何其他键,只按鼠标。它在那里是如何工作的?

我的最终目标是在同一个元素上调用相同的函数 onmouseup,但使用 Java 中的 selenium 库。我还在使用 xpath 获得的元素上使用 JavaScript Executor。它在那里也给出了同样的错误。 因此,如果这有效,它也应该在那里有效。

我已仔细检查是否选择了相同的元素而不是其他元素。 我可以做些什么来解决这个问题?

【问题讨论】:

  • 这不完全是事件触发器,你只是在调用事件处理函数。
  • 好的,那我该怎么办?我是这个领域的新手。还有,Trigger 和 call 的意思不一样呢?
  • 使用addEventListener附加事件,dispatchEvent触发事件。

标签: javascript mouseevent onmouseup


【解决方案1】:

就像@teemu 在评论中所说,您所做的不是事件触发器,而是对函数onmouseup() 的调用。问题是这个调用没有在你的例子中被称为b的事件参数所以n = b.metaKey || b.ctrlKeyn = undefined.metakey || undefined.ctrlKey

要创建你使用的 mouseup 事件:

newEvt = new MouseEvent("mouseup", { 气泡:真的, 可取消:真, screenX: , screenY: , clientX: , clientY: }); // screenX, screenY, clientX 和 clientY 是可选的,默认为 0 document.getElementsByClassName("one")[0].dispatchEvent(newEvt);

请参阅 MDN MouseEvent 文档:https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent


关于元键,它们对应于mac上的windows key或⌘key(来源:https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/metaKey

【讨论】:

  • 有人知道如何为 javascript sn-ps 添加颜色吗?
  • 非常感谢,您和@programmer 的回答共同帮助解决了问题。
【解决方案2】:

通过这种方式,您可以将事件与您需要的数据绑定(以修复错误,或出于任何其他原因)-

var e = new Event('mouseup');
document.getElementsByClassName("one")[0].trigger(e);

【讨论】:

  • 但是当我使用鼠标单击时,我没有按任何键(Enter 或 Win),为什么我需要在这里为 metaKey 分配一些值?
  • 我的错误。您不需要设置 metaKey。您的错误是因为事件元素未定义,而不是因为 metaKey 没有值。
  • 非常感谢,您和 jonatjano 的回答共同帮助解决了问题。
【解决方案3】:

onmouseover 和 onmouseout


< tr class="one" style="height:20px;" onmouseover ="click('first value','2nd value')"> Text </tr>

为了 onmousedown="mouseDown()" onmouseup="mouseUp()"

这里是例子

function mouseDown() {
    document.getElementById("myP").style.color = "red";
}

function mouseUp() {
    document.getElementById("myP").style.color = "green";
}
<p id="myP" onmousedown="mouseDown()" onmouseup="mouseUp()">
Click the text! The mouseDown() function is triggered when the mouse button is pressed down over this paragraph, and sets the color of the text to red. The mouseUp() function is triggered when the mouse button is released, and sets the color of the text to green.
</p>
<script>
function mouseDown() {
    document.getElementById("myP").style.color = "red";
}

function mouseUp() {
    document.getElementById("myP").style.color = "green";
}
</script>

【讨论】:

  • 我不确定这如何回答我的问题。
  • 你想做什么活动让我为你做
猜你喜欢
  • 2021-06-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-16
  • 2016-12-04
  • 2014-09-18
  • 1970-01-01
相关资源
最近更新 更多