如何编辑或调用此jQuery函数以在普通JavaScript onmouseout上工作? [英] How to edit or call this jQuery function to work on a vanilla JavaScript onmouseout?

查看:53
本文介绍了如何编辑或调用此jQuery函数以在普通JavaScript onmouseout上工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编辑或调用此jQuery .click()函数以将其用作常规或原始JavaScript onmouseout :

I need to edit or call this jQuery .click() function to use it as a regular or vanilla JavaScript onmouseout:


$("#copystuff").click(function() {
  var temp = $("<textarea>");
  $("body").append(temp);

  var previewHeader = $("#PreviewHeader").text();
  var HiddenURLdiv = $("#HiddenURLdiv").text();
  var contentTogether = previewHeader + "\n" + HiddenURLdiv;

  temp.val(contentTogether).select();
  document.execCommand("copy");
  $("#thecopiedtext").text(contentTogether);
  temp.remove();
});

这是因为我需要先使用onclick函数将一个div的内容随机化,该功能已经可以很好地工作了,所以请不要专注于此,而是将其内容与第二个div的内容一起复制可以使用此jQuery函数很好地完成工作,但是我需要第二步通过JavaScript onmouseout来实现,只需单击一下即可完成所有工作,就像这样:

This is because I need to first randomize the content of one div with an onclick function which is working perfectly fine already so please don't focus on this, but then copy its content together with the content of a second div which I can do perfectly fine with this jQuery function already, but I need this second step to be achieve it with a JavaScript onmouseout to have all this job done in one single click, like this:

  • onclick将一个div内容随机化(已解决,因此请跳过),
  • onmouseout复制两个div的内容.

因为将此jQuery的.click事件更改为.mouseout或.mouseleave或.mousemove仅触发打印(附加)功能,而不触发复制功能.实际上,复制功能仅在所有情况下都在单击后才会发生.

because changing the .click event of this jQuery to .mouseout or .mouseleave or .mousemove only triggers the printing (append) function, but not the copy one. Actually the copy function only happens after a click is done in all cases.

长话短说:我只需要通过常规或原始JavaScript onmouseout来执行此jQuery函数,即可编辑或调用此jQuery .

或者可以使以下mouseout事件在不单击其元素的情况下进行实际复制:


$("#copystuff").mouseout(function() {
  var temp = $("<textarea>");
  $("body").append(temp);

  var previewHeader = $("#PreviewHeader").text();
  var HiddenURLdiv = $("#HiddenURLdiv").text();
  var contentTogether = previewHeader + "\n" + HiddenURLdiv;

  temp.val(contentTogether).select();
  document.execCommand("copy");
  $("#thecopiedtext").text(contentTogether);
  temp.remove();
});

以下是我的两个常用JavaScript函数:

These are my two vanilla JavaScripts functions:

  • onclick ="randomize(divone)"
  • onmouseout ="copyText(bothdivs)" <-我需要使用此jQuery的那个
  • onclick="randomize(divone)"
  • onmouseout="copyText(bothdivs)" <-- the one I need to use this jQuery

谢谢大家.

推荐答案

我会像这样模拟onClick事件:

I would simulate onClick event on it like this:

document.querySelector("your element here").click()

尝试像这样使用它:

元素1是具有mouseout(mouseleave)事件的元素,元素2是要单击的元素,该元素调用所需的元素.

Element 1 is the element that has mouseout (mouseleave) event, and element 2 is the element that you want to be clicked, the one that calls what you need.

document.querySelector("element 1").addEventListener("mouseleave", ()=>{
   document.querySelector("element 2").click();
});

您甚至可以尝试将"mouseleave"更改为"mouseout"

You can even try changing "mouseleave" with "mouseout"

用您的代码实施我的解决方案:

Implementing my solution with your code:

document.querySelector("you put some element here that you want to add mouseleave event to").addEventListener("mouseleave", ()=>{
   document.querySelector("#copystuff").click();
});

这篇关于如何编辑或调用此jQuery函数以在普通JavaScript onmouseout上工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆